How to set up Go on WSL, Go environment and hello world Go application
- install Go from here
wget https://dl.google.com/go/go1.10.linux-amd64.tar.gz
sudo tar -C /usr/local -xzf go1.10.linux-amd64.tar.gz
- set up development folders
mkdir -p ~/go/src
mkdir -p ~/go/pkg
mkdir -p ~/go/bin
- set up GOPATH & GOBIN
add the following lines to ~/.bashrc
export GOPATH=$HOME/go
export GOBIN=$HOME/go/bin
export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin
- check Go version and Go environment
source ~/.bashrc
go version
go env
- create hello.go
cd ~/go/src
vi hello.go
package main
import "fmt"
func main() {
fmt.Printf("Hello, world.\n")
}
- run hello.go
go run hello.go # output: Hello, world.
- build and install hello.go
go build hello.go # create a new file hello on the same directory
./hello # output: Hello, world.
hello # -bash: /home/you/go/bin/hello: No such file or directory
go install hello.go # create a new file hello on ~/go/bin directory
hello # output: Hello, world.