How to import sub-routes from other packages

react router nested routes
angular routing
angular routerlink
angular 8 routing
angular 7 routing example
angular routing example
angular routing tutorial
angular router-outlet

Instead of writing every route under main(), like

func main() {
    e := echo.New()
    e.GET("/api", sayHello)
    e.GET("/api/music", getMusic)
    e.GET("/api/user/:id", getDetail)
    e.POST("/api/user", addUser)
    // ...
}

How can I import these all sub-routes from a file called api.go, and use those in the main function? Similar to

import "./API"
func main() {
    e := echo.New()
    e.UseSubroute(API.Routes) // <-- similar to this
    // ...
}

The Echo object does not have this method. I think you need the code?

API.go:

package main

import "github.com/labstack/echo"

func UseSubroute(echo *echo.Echo)  {
    echo.GET("/api", sayHello)
    echo.GET("/api/music", getMusic)
    echo.GET("/api/user/:id", getDetail)
    echo.POST("/api/user", addUser)
}

main.go:

package main

import "github.com/labstack/echo"

func main() {
    e := echo.New()
    UseSubroute(e)
}

These two files need to be placed in the same directory.

Do you need it?

In-app navigation: routing to views, Repeat this step for a second component but give it a different name. Here, the new name is app-routing.module'; // CLI imports AppRoutingModule import { AppComponent } from '. These types of nested routes are called child routes. Java Sub Package and Static Import. In this tutorial, we will learn about sub-packages in Java and also about the concept of static import and how it is different from normal import keyword. Subpackage in Java. Package inside the package is called the subpackage. It should be created to categorize the package further.

What you can do is use the echo.Group and pass it to the api package, and initialize the routes handler there.

package api

import (
    "github.com/labstack/echo"
)

func UseSubroute(group *echo.Group)  {
    group.GET("/", sayHello)
    group.GET("/music", getMusic)
    group.GET("/user/:id", getDetail)
    group.POST("/user", addUser)
}

and in the main you can just import your api package.

package main

import (
    "github.com/labstack/echo"
    "your-repo/path-to/api" // your api package
)

func main() {
    e := echo.New()
    apiGroup := e.Group("/api")
    api.UseSubroute(apiGroup)
    // ...
}

Allow components to define their routes in their definition file instead , If you use webpack you can import components into router.map() like this I think there are potentially two different ways to view sub-routes: 1) treat them @​mrjones2014 we (@javisperez and I) have released the package  If your package is thoroughly dependent on another package, so that you’re using all sorts of functions from that other package, I’d go ahead and use @import and import the entire namespace of the other package. If, on the other hand, you’re just using one or two functions, I’d use @importFrom to import just the particular functions.

Based on @Andy 's idea, I come up with a solution, that supports detachable nested routes.

The current folder structure is as follows:

.
├── routes
│   ├── index.go
│   └── music.go
└── server.go

...where server.go is the project main entry, belongs to the main package, while index.go and music.go belong to routes package.

The endpoints are

"/api"       -> index.go
"/api/music" -> music.go

First in index.go we define a function for using routes at this level.

func UseRoute(group *echo.Group, routes func(group *echo.Group)) {
    routes(group)
}

Then,

in server.go

func main() {
    e := echo.New()
    apiGroup := e.Group("/api")
    routes.ActivateIndex(mainGroup)
}

in index.go

var mainGroup *echo.Group
func ActivateIndex(g *echo.Group) {
    mainGroup = g
    UseRoute(mainGroup, IndexRoutes)

    // sub routes
    musicGroup := mainGroup.Group("/music")
    ActivateMusic(musicGroup)
}

and in music.go

var musicGroup *echo.Group
func ActivateMusic(g *echo.Group) {
    musicGroup = g
    UseRoute(musicGroup, MusicRoutes)
}

Note: IndexRoutes, MusicRoutes etc. are functions that specify endpoints at this level.

e.g.

func IndexRoutes(group *echo.Group) {
    group.GET("/", sayHello)
    group.GET("/user/:id", getDetail)
    group.POST("/user", addUser)
}

In this way, the routes can be defined in different .go files, making the business logic clearer.

For example, to extend the nested level, we can create another ActivateHiphop function in hiphop.go, also import the new sub-routes at ActivateMusic function from music.go, so that "/api/music/hiphop" can be pointed to hiphop.go.

p.s. To add more routes in /api level, just create more endpoints in IndexRoutes function.

The Economy of Ghana Sixty Years after Independence, Manufacturing, through the import-substitution route and manufacturing units Among other factors, this stimulated migration of people from the rural areas and​  Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question. Provide details and share your research! But avoid … Asking for help, clarification, or responding to other answers. Making statements based on opinion; back them up with references or personal experience. To learn more, see our tips on writing great

Add support for nested routes · Issue #262 · sveltejs/sapper · GitHub, It could be really helpful for Sapper to support nested routing, Code review · Project management · Integrations · Actions · Packages · Security · Team management · Hosting Links to how some of the other frameworks handle nested routing: await import('routes/settings/notifications/index.html'), await  How to import packages in Java? Java has an import statement that allows you to import an entire package (as in earlier examples), or use only certain classes and interfaces defined in the package. The general form of import statement is: import package.name.ClassName; // To import a certain class only import package.name.* // To import the

Geographic Information Systems: Concepts, Methodologies, Tools, , After the nodes of the sub routes of two solutions get exchanged in the The already existing nodes which become identical with the newly imported A few of the nodes of one route are exchanged with some othernodes of another route in  menu, select ' Export' then click on ' Package (.zip)'. On the next screen, provide a name and a description for your package first. You can then configure defaults and add comments in case which will be accessible during the import phase.

React router dom: Nested routes, In this tutorial, we are going to build a nested route, or commonly called in two very different ways and they look very similar in the browser. Prerequisites. knowledge of javascript and react. basic knowledge of command line to install npm packages. import {BrowserRouter} from "react-router-dom". In Object Explorer, expand the Stored Packages folder. Expand the subfolders to locate the folder into which you want to import a package. Right-click the folder, click Import Package. and then do one of the following: To import from an instance of SQL Server, select the SQL Server option, and then specify the server and select the authentication mode. If you select SQL Server Authentication, provide a user name and a password.

Comments
  • Files are not importable, in Go you can only import packages. And also do not use relative imports. I would suggest you follow the recommendations from here: golang.org/doc/code.html
  • @mkopriva Here api.go is included in the package: package API
  • I would also suggest you consider renaming your package (golang.org/doc/effective_go.html#package-names). That said, you can declare a function in your API package that takes *echo.Echo as its argument and does all the route-to-handler registering.
  • About package, I moved the source code to src/, ran go mod init, upload to Github, then I could use a canonical name starting with github.com/username/myapp/API. This feels terrible and very unnatural IMO, but this is Go, isn't it lol.
  • with go mod it is not necessary to have your package in src/, without go mod it is necessary that your package is located in $GOPATH/src, however it is not necessary to upload your package to github. And also, although it is a convention when using GOPATH, it is not necessary that your package is located in $GOPATH/src/github.com/user/repo, something like $GOPATH/src/api and then import "api" will work as well.
  • Thanks. This works for now. There is still a problem though when trying to achieve the same to /api/user/xxx. After all, it would be ugly to write func UseSubroute() in every sub-route file.