dongxie2756 2018-10-26 02:00
浏览 41
已采纳

在同一文件夹中命名文件空间

I have this structure:

$GOPATH/
     src/
       foo/
         main.go
         routes/
            register.go
            login.go

in main.go, I have:

import "foo/routes"

router.HandleFunc("/register", routes.RegisterNewUser).Methods("GET")
router.HandleFunc("/login", routes.Login).Methods("GET")

routes.RegisterNewUser is in the routes/register.go file routes.Login is in the routes/login.go file

how can I namespace RegisterNewUser and Login? Is the only way to do that, to put register.go and login.go in their own folders, like this:

routes/
  register/
     register.go
  login/
     login.go

Hopefully there is another way other than putting the files in their own folder which just makes noise when navigating for files.

  • 写回答

1条回答 默认 最新

  • doudu9652 2018-10-26 03:09
    关注

    There isn't any way to do what you want since packages are denoted by the import path, which is tied to the directory structure. One directory, one package (the exception being package routes_test, for testing purposes).

    In my observation many Go programmer seem to prefer larger packages, rather than subdividing things in smaller packages. I find that for e.g. HTTP handlers this is usually fine, as they tend to be fairly small functions, with the bulk of the logic being somewhere else, such as a "service", "model", or whatever you're naming it in your architecture.

    This is a matter of personal taste though, and some programmers do subdivide their code in to smaller packages by putting them in subdirectories, like you've done in your example.


    Another option might be to divide things in to structs:

    routes/login.go:

    package routes
    
    type LoginHandler struct {}
    
    func (h LoginHandler) Login(r *http.Request, w http.ResponseWriter) {
        // ...
    }
    
    // .. other methods related to Login etc. ..
    

    routes/register.go:

    package routes
    
    type RegisterHandler struct {}
    
    func (h RegisterHandler) NewUser(r *http.Request, w http.ResponseWriter) {
        // ...
    }
    
    // .. other methods related to registration etc. ..
    

    And then register that with e.g.:

    login := routes.Login{}
    router.HandleFunc("/login", login.Login).Methods("GET")
    
    register := routes.Register{}
    router.HandleFunc("/register", register.NewUser).Methods("GET")
    

    You can also add a Mount() or Routes() method to the LoginHandler and RegisterHandler types like so:

    type LoginHandler struct {}
    
    // Mount the routes.
    func (h LoginHandler) Mount(router someRouterType) {
        router.HandleFunc("/login", h.Login).Methods("GET")
    }
    

    Instead of putting them all in main.go. This, again, is a matter of personal taste.

    This is also a convenient way to inject common dependencies, such as a database connection.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 socket通信实现多人聊天室疑惑
  • ¥15 DEV-C++编译缺失
  • ¥33 找熟练码农写段Pyhthon程序
  • ¥100 怎么让数据库字段自动更新
  • ¥15 antv g6 力导向图布局
  • ¥15 quartz框架,No record found for selection of Trigger with key
  • ¥15 锅炉建模+优化算法,遗传算法优化锅炉燃烧模型,ls-svm会搞,后面的智能算法不会
  • ¥20 MATLAB多目标优化问题求解
  • ¥15 windows2003服务器按你VPN教程设置后,本地win10如何连接?
  • ¥15 求一阶微分方程的幂级数