Go(golang)包由几个文件夹组成。

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

Go (golang) Package consisting of several folders

问题

在Go(golang)中,是否可以将一个包组成多个来自不同文件夹的.go文件?

我试图在主文件夹内创建一个子文件夹,但编译器显示找不到定义("undefined"错误)。当所有文件都在同一个文件夹中时,编译不会出错。

谢谢。

英文:

Is it possible in Go (golang) to have a package consisting of several .go files from different folders?

I am trying to make a subfolder inside of the main folder, and compiler says that it cannot find definitions ("undefined" error). When all the files are in the same folder, compilation does not give errors.

Thanks.

答案1

得分: 38

不,这是不可能的。

如果你想在你的Go项目中使用文件夹,你必须使用多个包。

无效的结构

myproject
| -- main.go (包名:main)
| -- routes.go (包名:main)
+ -- models
     | -- db.go (包名:main)
     | -- mymodel.go (包名:main)

有效的结构

myproject
| -- main.go (包名:main)
| -- routes.go (包名:main)
+ -- models
     | -- db.go (包名:models)
     | -- mymodel.go (包名:models)
英文:

No, this is not possible.

If you want to use folders inside your go project you have to use several packages.

NOT VALID

myproject
| -- main.go (package main)
| -- routes.go (package main)
+ -- models
     | -- db.go (package main)
     | -- mymodel.go (package main)

VALID

myproject
| -- main.go (package main)
| -- routes.go (package main)
+ -- models
     | -- db.go (package models)
     | -- mymodel.go (package models)

huangapple
  • 本文由 发表于 2017年7月4日 04:24:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/44893435.html
匿名

发表评论

匿名网友

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

确定