简体   繁体   English

Golang调用/调用子项目的最佳方法

[英]Golang best way of calling/invoking sub project

Im moving my project from PHP to Golang and I looking efficient way of calling/invoking/handling control to sub project main.go from src main.go , I want to pass control from 林正从我的项目PHPGolang ,我看叫/调用/处理控制的有效途径sub project main.gosrc main.go ,我想从通过控制

http://localhost/  =>   http://localhost/sub-project1/
http://localhost/  =>   http://localhost/sub-project2/
http://localhost/  =>   http://localhost/sub-projectn/

I'm new to Golang I don't know how to do it in best way, and my project structure is 我是Golang我不知道如何以最佳方式进行操作,我的项目结构是

src/
    main.go
    sub-project1/
        main.go
    sub-project2/
        main.go
    sub-projectn/
        main.go
    gitHub.com/
        ......
    golang.org/
        ......

I'm using httprouter for routing, in main.go which is located under src contain following 我正在使用httprouter进行路由,位于src下的main.go ,包含以下内容

package main
import ....

// homePageHandler
// contactPageHandler
// aboutPageHandler
// loginPageHandler
// signupPageHandler

func main() {
    router := httprouter.New()
    router.GET("/", homePageHandler)
    router.GET("/contact", contactPageHandler)
    router.GET("/about", aboutPageHandler)
    router.GET("/login", loginPageHandler)
    router.GET("/signup", signupPageHandler)

    // here I want to pass control to my sub project main.go 
    // and I don't want to write any /sub-project routing urls here,
    // because each /sub-project's contain many urls 
    router.GET("/sub-project1", ??????)
    router.GET("/sub-project2", ??????)
    router.GET("/sub-project3", ??????)
    router.GET("/sub-projectn", ??????)
}

and all files must be passes from src main.go because whole project has only one main() and inside any /sub-projectx main.go I want to do this 并且所有文件都必须从src main.go传递,因为整个项目只有一个main()且在任何/sub-projectx main.go我想这样做

package main
import ....
// subprojectPageHandler
// feature1PageHandler
// feature2PageHandler
// feature3PageHandler
// ........
// featurenPageHandler

func main() {
    router := httprouter.New()
    router.GET("/sub-projectx", subprojectPageHandler)
    router.GET("/sub-projectx/feature1", feature1PageHandler)
    router.GET("/sub-projectx/feature2", feature2PageHandler)
    router.GET("/sub-projectx/feature3", feature3PageHandler)
    ..........
    router.GET("/sub-projectx/featureN", featureNPageHandler)


    // here I want to pass control to my sub project main.go 
    // and I don't want to write any /sub-project routing urls here,
    // because each /sub-project's contain many urls 
    router.GET("/sub-project1", ??????)
    router.GET("/sub-project2", ??????)
    router.GET("/sub-project3", ??????)
    router.GET("/sub-projectn", ??????)
}

To be 成为

Golang source code is not running through interpreter, but is built into a binary, which gives less flexibility in case of dynamic projects. Golang源代码不是通过解释器运行的,而是内置在二进制文件中的,在动态项目的情况下灵活性较低。 That said, I'd keep my projects isolated one from another, and would let Nginx (for example) take care of multiple project grouping. 就是说,我会将我的项目彼此隔离,并让Nginx (例如)负责多个项目分组。 Of course, that would require some refactoring like creating shared packages, etc. 当然,这将需要一些重构,例如创建共享软件包等。

Or not to be 还是不被

If, for some reason, you still think running multiple projects via single binary is ok, it's your choice. 如果由于某种原因,您仍然认为通过单个二进制文件运行多个项目是可以的,那是您的选择。 In this case you might have a look into route grouping that are available in some frameworks. 在这种情况下,您可能会研究某些框架中可用的路由分组。 Here's what Go Gin provides : 是Go Gin提供的

func main() {
    router := gin.Default()

    // Simple group: v1
    v1 := router.Group("/v1")
    {
        v1.POST("/login", loginEndpoint)
        v1.POST("/submit", submitEndpoint)
        v1.POST("/read", readEndpoint)
    }

    // Simple group: v2
    v2 := router.Group("/v2")
    {
        v2.POST("/login", loginEndpoint)
        v2.POST("/submit", submitEndpoint)
        v2.POST("/read", readEndpoint)
    }

    router.Run(":8080")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM