英文:
Organizing a multiple-file Go project with private support files
问题
根据你提供的信息,以下是翻译好的内容:
根据这个答案,我创建了以下的项目结构:
.
├── bin
├── pkg
└── src
└── github.com
└── GITHUB_USERNAME
└── PROJECTNAME
├── lib
│ └── model.go
│ └── ... .go
├── LICENSE
├── README.md
└── PROJECTNAME.go
PROJECTNAME.go
文件的包名是main
model.go
文件的包名是PROJECTNAME
在 PROJECTNAME.go
文件中,我导入了以下内容:
import(
'github.com/GITHUB_USERNAME/PROJECTNAME/lib/model'
)
但是当我运行 go build
时,出现了以下错误:
cannot find package "github.com/GITHUB_USERNAME/PROJECTNAME/lib/model" in any of:
/usr/lib/go/src/pkg/github.com/GITHUB_USERNAME/PROJECTNAME/lib/model (from $GOROOT)
/home/USERNAME/go/src/github.com/GITHUB_USERNAME/PROJECTNAME/lib/model (from $GOPATH)
为了正确导入包,包名应该如何设置?还有其他的策略吗?
英文:
Following this answer I have created the following project structure:
.
├── bin
├── pkg
└── src
└── github.com
└── GITHUB_USERNAME
└── PROJECTNAME
├── lib
│   └── model.go
│   └── ... .go
├── LICENSE
├── README.md
└── PROJECTNAME.go
PROJECTNAME.go
has the packagemain
model.go
has the packagePROJECTNAME
In the PROJECTNAME.go
I am importing the follwing:
import(
'github.com/GITHUB_USERNAME/PROJECTNAME/lib/model'
)
but when I run go build
I get the follwing error:
cannot find package "github.com/GITHUB_USERNAME/PROJECTNAME/lib/model" in any of:
/usr/lib/go/src/pkg/github.com/GITHUB_USERNAME/PROJECTNAME/lib/model (from $GOROOT)
/home/USERNAME/go/src/github.com/GITHUB_USERNAME/PROJECTNAME/lib/model (from $GOPATH)
How must the packages names be to import correctly? Are there any other strategies?
答案1
得分: 1
这里有两个问题:
- 你在导入时漏掉了"project"。使用
github.com/GITHUB_USERNAME/PROJECTNAME/lib/model
。 - 小问题:将包
github.com/GITHUB_USERNAME/PROJECTNAME/lib/model
命名为"PROJECTNAME"似乎有些奇怪。可以考虑使用lib或model。
英文:
Two things here:
- You missed "project" during the import. Use
github.com/GITHUB_USERNAME/PROJECTNAME/lib/model
- Minor: Naming the package github.com/GITHUB_USERNAME/PROJECTNAME/lib/model "PROJECTNAME" seems strange. How about lib or model?
答案2
得分: 1
你的主包的导入语句应该是这样的:
import(
"github.com/GITHUB_USERNAME/PROJECTNAME/lib/PROJECTNAME"
)
你说你的 model.go 文件使用 PROJECTNAME 作为包名。所以在导入路径中,实际上不需要指定文件名。最后一个组件应该是包名。这意味着你可以在 lib 目录下有多个以 PROJECTNAME 作为包名的 go 文件。
英文:
Your import statement from your main package should read
import(
"github.com/GITHUB_USERNAME/PROJECTNAME/lib/PROJECTNAME"
)
You said your model.go uses PROJECTNAME as the package. So you don't actually name a file in your import path. The last component should be the package name. That means you can have a number of go files in the lib directory all with PROJECTNAME as the package.
答案3
得分: 1
包导入路径由目录名定义,而不是文件名。
正确的导入路径是"github.com/GITHUB_USERNAME/PROJECTNAME/lib"
,该文件夹中的所有go文件在顶部必须具有相同的package
子句。
包子句后面的标识符是将被导入到导入它的包中的标识符。因此,如果是package foo
,您可以通过在导入代码中使用foo.Bar
来访问Bar
标识符。
按照惯例,作者通常使用导入路径的最后一部分作为package
名称,所以在这种情况下,您应该在lib文件夹下的所有Go文件的顶部都有package lib
。
英文:
Package import paths are defined by directory names, not file names.
The correct import path is "github.com/GITHUB_USERNAME/PROJECTNAME/lib"
and all of the go files in that folder must have the same package
clause at the top.
The identifier after the package clause is what identifier will be imported into the package that imports it. So if it's package foo
, you can access the Bar
identifier by foo.Bar
in the importing code.
By convention, authors typically use the last part of the import path as the package
name, so in this case, you should have package lib
at the top of all of the Go files under the lib folder.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论