构建和安装 Golang 项目以及一些内部包。

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

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

huangapple
  • 本文由 发表于 2014年6月20日 22:48:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/24330011.html
匿名

发表评论

匿名网友

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

确定