Go 无法调用 NewRouter() 函数

Go cannot call NewRouter() function

我是 Go 的新手,但我正在尝试创建一个 RESTful API 使用 Gorilla Mux 来创建基于这篇文章的路由器 http://thenewstack.io/make-a-restful-json-api-go/

我有一个包含以下代码的路由器文件。

package main

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

type Route struct {
    Name        string
    Method      string
    Pattern     string
    HandlerFunc http.HandlerFunc
}

type Routes []Route

func NewRouter() *mux.Router {

    router := mux.NewRouter().StrictSlash(true)
    for _, route := range routes {
        router.
            Methods(route.Method).
            Path(route.Pattern).
            Name(route.Name).
            Handler(route.HandlerFunc)
    }
    return router
}

var routes = Routes{
    Route{
        "Index",
        "GET",
        "/",
        Index,
    },
}

在我的 Main.go 我有这个:

package main

import (
    "log"
    "net/http"
)

func main() {
    router := NewRouter()
    log.Fatal(http.ListenAndServe(":8080", router))
}

根据我对 Go 的了解以及如何从一个文件调用另一个文件中的方法,这应该可行。但是当我 运行: go build Main.go 我在我的控制台中得到这个错误:

go run Main.go
# command-line-arguments
./Main.go:10: undefined: NewRouter

我已经 运行 go get 在我的 src 文件夹中,其中包含我的所有文件以获取大猩猩,但这并没有解决它。我在这里做错了什么?

如果您的 main 包包含多个 .go 文件,您必须将所有文件传递给 go run,例如:

go run Main.go Router.go