Does not make sense that I have to have files before import

后端 未结 3 1694
無奈伤痛
無奈伤痛 2021-01-14 07:55

How do I import an external package from scratch?

I\'ve written a library package in Go and testing to distribute through github. I am following http://golang.org/do

3条回答
  •  情书的邮戳
    2021-01-14 08:48

    Go is a static type language thus it needs to resolve any reference to external package at compile time. The "go" tool expects the source of external packages in locally accessible path thus you need to use "go get" to download them.

    From what you described, you probably did not set the GOPATH. Use ECHO $GOPATH to check if it is set correctly.

    For my GO project, I normally use GOPATH as workspace, similar to virtualenv in Python or rbenv/rvm in Ruby. Let say my project "myproject" has root at /projects/myproject, my source file will be located at /projects/myproject/src/myproject and there is an import of "github.com/user/project", then

    > cd /projects/myproject
    > export GOPATH=`pwd`          # or export GOPATH=/projects/myproject
    > go get github.com/user/project
    

    After "go get" command, the source of "github.com/user/project" will be downloaded to /projects/myproject/src/github.com/user/project.

    When you use "go build" or "go install" then, it will compile as the external packages is in the $GOPATH/src folder.

    If you install Go in the default folder, you need to include Go installed bin folder in the PATH environment variable. After that GOPATH is the other environment variable you need for "go" tool to work.

提交回复
热议问题