运行多文件的Go程序

huangapple go评论78阅读模式
英文:

Running multi-file go program

问题

所以我对Go语言还不太熟悉,我正在尝试按照这个教程进行操作 -

http://thenewstack.io/make-a-restful-json-api-go/

现在,这是我的文件结构 -

EdData/
    dataEntry/
       populateDb.go
    main.go
    handlers.go
    routes.go

当我运行 go run main.go 时,我得到了这个错误 ./main.go:11: undefined: NewRouter

这是我的 main.go 文件的内容 -

package main 

import (
	"net/http"
	"log"
)



func main() {
	router := NewRouter()

	log.Fatal(http.ListenAndServe(":8080", router))

}

func checkErr(err error) {
    if err != nil {
        panic(err)
    }
}

这是我的 routes.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,
	},
}

这是我的 handlers.go 文件的内容

package main

import (
	"fmt"
	"net/http"
)

func Index(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintln(w, "WELCOME!")
}

当我尝试构建 routes.go 时,我得到了 Index 未定义的错误,当我尝试构建 handlers.go 时,我得到了

# command-line-arguments runtime.main: undefined: main.main

我该如何运行它?还有,我在哪里执行 go run 命令?我需要手动构建所有依赖的文件吗?

英文:

So I'm pretty new to go and I'm trying to follow this tutorial -

http://thenewstack.io/make-a-restful-json-api-go/

Right now, this is my file structure -

EdData/
    dataEntry/
       populateDb.go
    main.go
    handlers.go
    routes.go

When I run go run main.go, I get this error ./main.go:11: undefined: NewRouter

This is what my main.go looks like -

package main 

import (
	"net/http"
	"log"
)



func main() {
	router := NewRouter()

	log.Fatal(http.ListenAndServe(":8080", router))

}

func checkErr(err error) {
    if err != nil {
        panic(err)
    }
}

This is what my routes.go looks like

    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,
	},
}

and this is what my handlers.go looks like

package main

import (
	"fmt"
	"net/http"
)

func Index(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintln(w, "WELCOME!")
}

When I try and build routes.go, I get that Index is undefined, and when I try and build handlers.go, I get

# command-line-arguments
runtime.main: undefined: main.main

How do I get this to run? Also, where do I execute the go run command? Do I need to manually build all the dependent files?

答案1

得分: 2

go run的帮助信息中可以得知:

用法:run [编译标志] [-exec xprog] gofiles... [参数...]

run命令编译并运行由指定的Go源文件组成的主包。
Go源文件的定义是以字面上的".go"后缀结尾的文件。

只有传递给go run的文件才会被包含在编译中(不包括导入的包)。因此,在使用go run时,应该指定所有的Go源文件:

go run *.go

或者

go run main.go handlers.go routes.go

英文:

From the go run help:

usage: run [build flags] [-exec xprog] gofiles... [arguments...]

Run compiles and runs the main package comprising the named Go source files.
A Go source file is defined to be a file ending in a literal ".go" suffix.

Only the files passed to go run will be included in the compilation (excluding imported packages). Therefore, you should specify all of your Go source files when using go run:

go run *.go
# or
go run main.go handlers.go routes.go

huangapple
  • 本文由 发表于 2015年6月11日 04:38:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/30766840.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定