我无法初始化一个结构体,即使它没有显示任何检查错误,它在编译时抛出错误
错误$ go run main.go
# command-line-arguments
.main.go:19:10: undefined: Application
结构体:
/* app.go */
type Application struct {
config struct {
port string // to be read from the command line flag
env string // to be read from the command line flag
api string // to be read from the command line flag
stripe struct {
secret string // to be read from the environment variable
key string // to be read from the environment variable
}
}
infoLog *log.Logger
errorLog *log.Logger
templateCache map[string]*template.Template
version string
DB *sql.DB
Router *mux.Router
}
我是这样初始化它的
/* main.go */
app := Application{}
代码完整代码在这里
非常感谢您的帮助,谢谢!
试着运行go run .
(这里你传递的是文件夹而不是文件)或go run main.go app.go routes.go
。
如果你只是运行go run main.go
,它将只加载主。Go,它没有任何对其他文件的直接引用,所以它找不到其他文件中定义的结构体。通过传递包含所有主包文件的文件夹或显式传递所有文件,就不会产生混淆。