Go, Golang : does not make sense that I have to ha

2019-02-19 15:27发布

问题:

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/doc/code.html and using mac but getting error message

cmd I put is following.

$ mkdir $HOME/go
$ export GOPATH=$HOME/go
$ export PATH=$PATH:$GOPATH/bin
$ mkdir -p $GOPATH/src/github.com/user
$ mkdir $GOPATH/src/github.com/user/project

Then I put

$ go get github.com/user/project

Still errors with go install

can't load package: package github.com/golingorg/goling: no Go source files in /Users/user_name/go/src/github.com/user/project

I do not understand why we need files to import an external package in Go. External package means that I get something and create files from the external package.

My question is how I import an external package from scratch. Most of documents just say something like

 go get github.com/yasushi-saito/fifo_queue

this gives me "$GOPATH not set." I am getting frustrated setting up the environment for "go get" to work, as a beginner. Thanks a lot in advance.

Summary

   How do I import an external package from scratch?

回答1:

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.



回答2:

That's how I done it:

1. Setup your workspace first

 mkdir $HOME/go
 export GOPATH=$HOME/go
 export PATH=$PATH:$GOPATH/bin

2. Create the project

 mkdir -p $GOPATH/src/github.com/user
 mkdir $GOPATH/src/github.com/user/hello
 touch $GOPATH/src/github.com/user/hello/hello.go

3. Install it

go install github.com/user/hello

4. Run it

cd $GOPATH/bin
./hello

I used the following vagrant image: https://github.com/dcoxall/vagrant-golang



回答3:

From the help output for go get, it says:

By default, get uses the network to check out missing packages but does not use it to look for updates to existing packages.

When you created the $GOPATH/src/github.com/user/project directory prior to running go get, it assumed that the package had already been downloaded so skipped to the step of trying to build and install the package. That failed because the directory contained no Go source files.

So the simple fix is to not create the folder associated with the package you are trying to download: go get will do that for you.