英文:
Build and install golang project with some internal packages
问题
我有以下的golang项目结构:
- go-projects
- src
- github.com
- user
- my-project
- pack
- pack.go
- main.go
我的GOPATH
是:
export GOPATH=/home/user/go-projects
我试图在main.go
文件中调用pack
包中的函数,并尝试使用以下命令构建该项目:
cd my-project
go build && go install
没有任何输出,也没有任何地方有bin
目录。我做错了什么?
更新 这个问题已经解决了。我误解了main.go和pack.go文件的内容。但现在又出现了另一个问题。
如何正确构建这个程序。当我尝试在/home/user/go-projects/src/github.com/user/my-project
目录下执行go build
时,我得到以下错误:
main.go:4:8: cannot find package "github.com/user/pack" in any of:
/home/user/Downloads/go/src/pkg/github.com/user/pack (from $GOROOT)
/home/user/go-projects/src/github.com/user/pack (from $GOPATH)
谢谢。
英文:
I have following golang project structure:
- go-projects
- src
- github.com
- user
- my-project
- pack
- pack.go
- main.go
my GOPATH
is:
export GOPATH=/home/user/go-projects
I'm trying to call functions from pack
package in main.go
file and trying to build this project with:
cd my-project
go build && go-install
There is no any output, and also there is no bin
directory anywhere. What i did wrong?
UPD This problem was solved. accidently missunderstood file content of main.go and pack.go. But now there is another problem.
How to build correctly this program. When i'm trying to execute go build in /home/user/go-projects/src/github.com/user/my-project
i'm getting following erros:
main.go:4:8: cannot find package "github.com/user/pack" in any of:
/home/user/Downloads/go/src/pkg/github.com/user/pack (from $GOROOT)
/home/user/go-projects/src/github.com/user/pack (from $GOPATH)
Thank you
答案1
得分: 3
虽然让工具从当前工作目录推断包可能很方便,但它只适用于简单的main
包。习惯使用完整的导入路径引用包,这样可以避免后续出现其他问题。
go install github.com/user/pack
英文:
While it may be convenient to let the tools infer the package from the current working directory, it doesn't work for much more than a simple main
package. Get used to referencing packages by their full import path, and you'll save yourself other problems down the line.
go install github.com/user/pack
答案2
得分: 1
它会进入$GOPATH/bin/my-project
目录,如果你想立即进行测试,只需使用go run main.go
命令。
英文:
It goes into $GOPATH/bin/my-project
, if you wanna test it right away just use go run main.go
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论