#Setting up a Go development environment
I’m keeping notes on how I set up my development environment for each language.
First up is Go, which I installed recently.
##In this article
- Install the Go programming environment and run Hello World
- I set up a development environment in vim using vim-go.
##Installing Go
I remember struggling to get C and Java working. Setting up a newer language feels much simpler by comparison.
I was really able to easily set up the environment for Go language.
Access the URL below and download the installer compatible with the operating system you are using.
Since I am using Ubuntu 18.04LTS, I downloaded the Linux version (when writing: go1.13.5.linux-amd64.tar.gz).
For the Linux version, please execute the following command at the download location afterwards.
tar -C /usr/local -xzf go1.13.5.linux-amd64.tar.gz
Is it okay to install the Windows version by launching the msi file?
I think the macOS version is probably the same, but I honestly don't know much because I haven't used macOS.
Then, make sure to pass the path.
Add the following notation to your .bashrc file.
export PATH=$PATH:/usr/local/go/bin
After the installation is complete, run the following command to verify the functionality.
go version
> go version go1.13.5 linux/amd64
Once the version is displayed in this manner, the installation is complete.
Next, let's run a Hello World program in Go.
Create a file and name it hello.go.
package main
import "fmt"
func main() {
fmt.Printf("Hello world from Go!")
}
Create a file, save it, then run the following command.
go run hello.go
> Hello World from Go!
If it displays like this, compiling and running are OK.
##Setting GOPATH
Go has convenient commands for downloading source code from repositories on the web and building it.
When using commands like go get, you need to set the directory where to save the source code and executable files.
This directory is set as the environment variable $GOPATH.
Add the following text to ~/.profile or ~/.bashrc.
export GOPATH="$HOME/work" //好きなフォルダを設定
export PATH="$GOPATH/bin:$PATH"
The executable files are stored in the bin folder under GOPATH, so it's convenient to specify the PATH beforehand.
ℹ️ Info: There is also information that you don't need to specify GOPATH from Go 1.13.
I'm a bit unclear on that part, so I would appreciate it if you could explain it to me in detail.
##Installing vim-go
Until a little while ago, I used Visual Studio Code when writing code, but lately I've been using vim a lot.
I'm not very familiar with it yet, but I feel that the ease of access from the command line environment is a major advantage.
Therefore, I want to set up a development environment for the Go language in vim as well.
This time, I will introduce the vim-go plugin.
Run the following command to install it.
git clone https://github.com/fatih/vim-go.git ~/.vim/pack/plugins/start/vim-go
Then, start vim and execute the following command to install various binary files.
:GoInstallBinaries
Once the installation is complete, close vim once and create a new Go file using the following command.
vim test.go
By default, the following description is generated when created.
package main
import "fmt"
func main() {
fmt.Println("vim-go")
}
Let's try running it on vim.
Run the following command in vim.
:GoRun
If it displays "vim-go", that's okay.
Now my Go language development environment is set up using vim.
The plugin has plenty more features I haven’t explored. I’ll add notes as I find ones I use.
Info: Added on 2020/5/20
After that, I started using NeoVim as my main editor, and now when I write Go code, I have settled on using the ALE+Coc.vim+GoImports setup.
Eventually, I want to write articles about this environment as well.
##Wrapping up
I now have a working Go environment.
I’m still learning the language, so I’ll keep notes on the things that trip me up.