我是 Go 新手,但我正在尝试使用 Gorilla Mux 创建一个 RESTful API,以根据这篇文章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 in my src 文件夹,其中包含我的所有文件以获取 gorilla,但这并没有解决它。我在这里做错了什么?