[Environment variables] The directory where packages are installed by go get is $ GOPATH/bin. If not set, enter $ HOME/go/bin.

Hi, this is Takafumi. I have been developing LINE Bot with Go and company synchronization for a while.

I ran the command after doing go get to install the libraries used for development, but I got the message wire: not found.

** "Where is the one installed by go get installed? "** So, this time I would like to summarize what I checked about the installation destination by go get and $ GOPATH. think!

■ Conclusion

If $ GOPATH is not set, it will be installed in $ HOME/bin. Libraries installed with go get will be installed in $ GOPATH/bin.

■ Run go get github.com/google/wire/cmd/wire

Run go get to install the DI library" wire ". When the wire command is executed, it is displayed as follows.

/go/src/github.com/go-server-dev # wire
ash: wire: not found

I thought it was because the path didn't pass. Then I got the question ** "Where is the one installed by go get installed? "**.

■ Check with echo $ GOPATH

When I looked it up, I found that it was installed in $ GOPATH/bin, so I checked $ GOPATH with echo.

echo $GOPATH

Nothing came out. I certainly don't remember setting it.

■ What is $ GOPATH in the first place?

The GOPATH environment variable specifies the location of your workspace. Quote: https://github.com/golang/go/wiki/SettingGOPATH

$ GOPATH is ** workspace location **. Also, from go help gopath, it was stated that packages installed with go get will be installed in the directory set with $ GOPATH.

If so, where is it installed in my environment where $ GOPATH is not set? .. ..

■ If not set, it will be installed in $ HOME/go

It was written on the wiki on github.

If no GOPATH is set, it is assumed to be $HOME/go on Unix systems and %USERPROFILE%\go on Windows. https://github.com/golang/go/wiki/SettingGOPATH

If not set, it seems to be considered as $ HOME/go. After checking, I confirmed that the package "wire" installed this time is in $ HOME/go/bin.

/go/src/github.com/go-server-dev # echo $HOME
/root
/go/src/github.com/go-server-dev # ls /root/go/
bin  pkg  src
/go/src/github.com/go-server-dev # ls /root/go/bin/
wire

■ Set $ GOPATH

Set $ GOPATH properly.

/go/src/github.com/go-server-dev # vim ~/.bash_profile

↓ Described in vim
---
export GOPATH=$HOME/go
---

/go/src/github.com/go-server-dev # source ~/.bash_profile
/go/src/github.com/go-server-dev # echo $GOPATH
/root/go

I was able to confirm that the path set with echo $ GOPATH is displayed.

Summary: It seems better to set $ GOPATH explicitly

In order to know where to install with go get, it seems better to set $ GOPATH before starting development. Don't forget that if you don't set it, it will be installed in $ HOME/go!

Well then!

reference

SettingGOPATH https://github.com/golang/go/wiki/SettingGOPATH

GOPATH can be decided appropriately https://qiita.com/yuku_t/items/c7ab1b1519825cc2c06f

GOPATH settings and go get http://kodama-tech.hatenablog.com/entry/2016/12/14/002115

[Note] About GOPATH and packages of Go language https://qiita.com/chano2/items/ea76cc503e651f07bfb0

Recommended Posts

[Environment variables] The directory where packages are installed by go get is $ GOPATH/bin. If not set, enter $ HOME/go/bin.