How to set $ GOPATH in existing folder?

0

I want to install a package in a project on go

go get -u github.com/gorilla/mux

But type this command in 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 to set terminal in this existing folder where I am writing project.

    
asked by anonymous 01.02.2018 / 23:56

1 answer

1

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

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

Notice that this is different from most other languages, which has a folder for each project and saves the files / libraries / binaries in the folder of their respective projects.

See the example file organization 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 think you should set the $ 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 the project, since the commands in Go will search everything on the page you set $ GO_PATH

    
02.02.2018 / 02:01