组织一个包含多个文件的Go项目,并使用私有支持文件。

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

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 package main
  • model.go has the package PROJECTNAME

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

这里有两个问题:

  1. 你在导入时漏掉了"project"。使用github.com/GITHUB_USERNAME/PROJECTNAME/lib/model
  2. 小问题:将包github.com/GITHUB_USERNAME/PROJECTNAME/lib/model命名为"PROJECTNAME"似乎有些奇怪。可以考虑使用lib或model。
英文:

Two things here:

  1. You missed "project" during the import. Use github.com/GITHUB_USERNAME/PROJECTNAME/lib/model
  2. 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.

huangapple
  • 本文由 发表于 2014年2月13日 00:26:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/21733704.html
匿名

发表评论

匿名网友

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

确定