doulongdan2264 2013-10-14 08:53
浏览 52
已采纳

如何使用大猩猩/ mux在多个文件中拆分URL?

My directory structure looks like this:

myapp/
|
+-- moduleX
|      |
|      +-- views.go
|
+-- start.go

The app gets started with start.go and from there I configure all the routes and import the handlers from moduleX/views.go like this:

package main

import (
    "net/http"
    "github.com/gorilla/mux"
    "myapp/moduleX"
)

func main() {
    r := mux.NewRouter()
    http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./templates/static/"))))
    r.HandleFunc("/", moduleX.SomePostHandler).Methods("POST")
    r.HandleFunc("/", moduleX.SomeHandler)
    http.Handle("/", r)
    http.ListenAndServe(":8080", nil)
}

Now I want to add more modules and ask myself if (and how) it is possible to define the urls in the module in a urls.go file and somehow "import" them in start.go. Specifically I want start.go to know all the URLs in all the somemodule/urls.go files with just one import or some kind of a module.GetURLs function.

  • 写回答

2条回答 默认 最新

  • down100009 2013-10-14 08:59
    关注

    EDIT:

    To create a group of mux.Route's in one go, you could define a custom type (handler in the example below) and do do something like:

    package main
    
    import (
        "fmt"
        "github.com/gorilla/mux"
        "net/http"
    )
    
    type handler struct {
        path    string
        f       http.HandlerFunc
        methods []string
    }
    
    func makeHandlers(hs []handler, r *mux.Router) {
        for _, h := range hs {
            if len(h.methods) == 0 {
                r.HandleFunc(h.path, h.f)
            } else {
                r.HandleFunc(h.path, h.f).Methods(h.methods...)
            }
        }
    }
    
    // create some example handler functions
    
    func somePostHandler(w http.ResponseWriter, r *http.Request) {
        fmt.Fprint(w, "POST Handler")
    }
    
    func someHandler(w http.ResponseWriter, r *http.Request) {
        fmt.Fprint(w, "Normal Handler")
    }
    
    func main() {
        //define some handlers
        handlers := []handler{{path: "/", f: somePostHandler, methods: []string{"POST"}}, {path: "/", f: someHandler}}
        r := mux.NewRouter()
        http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./templates/static/"))))
        // Initialise the handlers
        makeHandlers(handlers, r)
        http.Handle("/", r)
        http.ListenAndServe(":8080", nil)
    }
    

    Playground

    ORIGINAL ANSWER:

    You don't need to import them if they're in the same package.

    You can define the URL variables in urls.go, and then the logic in views.go (or another file in package moduleX) as long as they have the same package declaration.

    For instance:

    // moduleX/urls.go
    
    package moduleX
    
    var (
        urls = []string{"http://google.com/", "http://stackoverflow.com/"}
    )
    

    Then:

    // moduleX/views.go (or some other file in package moduleX)
    
    package moduleX
    
    func GetUrls() []string {
        return urls
    }
    

    Then:

    // start.go
    
    package main
    
    import (
        "fmt"
        "myapp/moduleX"
    )
    
    func main() {
        for _, url := range moduleX.GetUrls() {
            fmt.Println(url)
        }
    }
    

    Or, even easier, just export the variable from the moduleX package by giving it a capitalised name.

    For instance:

    // moduleX/urls.go
    
    package moduleX
    
    var URLs = []string{"http://google.com/", "http://stackoverflow.com/"}
    

    and then:

    // start.go
    
    package main    
    
    import (
        "fmt"
        "myapp/moduleX"
    )
    
    func main() {
        for _, url := range moduleX.URLs {
            fmt.Println(url)
        }
    }
    

    Have a look at any of the Go source to see how they handle the same problem. A good example is in the SHA512 source where the lengthy variable is stored in sha512block.go and the logic is in sha512.go.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)