How to set $GOPATH to existing folder?

Asked

Viewed 218 times

0

I want install one bundle in a project in go

go get -u github.com/gorilla/mux

But to the type that commando in the terminal of Ubuntu

package github.com/gorilla/mux: cannot download, $GOPATH not set. For more details see: go help gopath

I’d like to know how do I do for set $GOPATH in that existing folder which I’m in writing the project.

  • Type in the terminal go env and put the result...

1 answer

1

$GO_PATH is the enviroment variable that defines where all your things related to Go will be, including

  • All bilbiotecas you download.
  • All project source code.
  • All binary files resulting from the compilation of your projects' source code.

Note that this is different from most languages, which have a folder for each project and store the/libraries/binaries in the folder of their respective projects.

See the example of file organization taken from official documentation:

bin/
    hello                          # command executable
    outyet                         # command executable
pkg/
    linux_amd64/
        github.com/golang/example/
            stringutil.a           # package object
src/
    github.com/golang/example/
        .git/                      # Git repository metadata
    hello/
        hello.go               # command source
    outyet/
        main.go                # command source
        main_test.go           # test source
    stringutil/
        reverse.go             # package source
        reverse_test.go        # test source
    golang.org/x/image/
        .git/                  # Git repository metadata
    bmp/
        reader.go              # package source
        writer.go              # package source
    #... (many more repositories and packages omitted) ...

Now, if you still find it best to set $GO_PATH to your project folder:

1 - Navigate to the desired folder

2 - Enter the command:

$ echo $PWD >> ~/.bash_profile
$ source ~/.bash_profile

Remember that you will have to do this every time you change your project, as the Go commands will look for everything on the page you set $GO_PATH

  • bash_profile is my username?

  • No. It is a file that is inside your /home/<user name> folder (The shortcut to this folder is "~/". Try typing "cd ~/" into the terminal.). . bash_profile is a special file that runs whenever your user logs into a terminal.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.