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

Sub*_*ito 5 url go mux

我的目录结构如下所示:

myapp/
|
+-- moduleX
|      |
|      +-- views.go
|
+-- start.go
Run Code Online (Sandbox Code Playgroud)

应用程序start.go从那里开始,我从那里配置所有路由并从这里导入处理程序moduleX/views.go:

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)
}
Run Code Online (Sandbox Code Playgroud)

现在我想添加更多模块,并问自己是否(以及如何)可以在urls.go文件中定义模块中的URL 并以某种方式"导入"它们start.go.具体来说,我想start.go知道所有somemodule/urls.go文件中的所有URL 只有一个导入或某种module.GetURLs功能.

Int*_*net 3

编辑:

mux.Route要一次性创建一组,您可以定义一个自定义类型(handler在下面的示例中)并执行以下操作:

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)
}
Run Code Online (Sandbox Code Playgroud)

操场

原答案:

import如果它们位于同一个包中,则不需要它们。

您可以在 中定义 URL 变量urls.go,然后在 中定义逻辑views.go(或 中的另一个文件package moduleX),只要它们具有相同的package声明即可。

例如:

// moduleX/urls.go

package moduleX

var (
    urls = []string{"http://google.com/", "http://stackoverflow.com/"}
)
Run Code Online (Sandbox Code Playgroud)

然后:

// moduleX/views.go (or some other file in package moduleX)

package moduleX

func GetUrls() []string {
    return urls
}
Run Code Online (Sandbox Code Playgroud)

然后:

// start.go

package main

import (
    "fmt"
    "myapp/moduleX"
)

func main() {
    for _, url := range moduleX.GetUrls() {
        fmt.Println(url)
    }
}
Run Code Online (Sandbox Code Playgroud)

或者,更简单的是,只需moduleX给变量一个大写的名称,即可从包中导出该变量。

例如:

// moduleX/urls.go

package moduleX

var URLs = []string{"http://google.com/", "http://stackoverflow.com/"}
Run Code Online (Sandbox Code Playgroud)

进而:

// start.go

package main    

import (
    "fmt"
    "myapp/moduleX"
)

func main() {
    for _, url := range moduleX.URLs {
        fmt.Println(url)
    }
}
Run Code Online (Sandbox Code Playgroud)

查看任何 Go 源代码,看看他们如何处理相同的问题。一个很好的例子是在源代码SHA512,长变量存储在 中sha512block.go,逻辑位于 中sha512.go