Learning Golang 01

#Learning Golang notebook –001

How to install golang

Set go workspace

Go still expects there to be a single workspace for third-party Go tools installe via go install.By default,this workspace is located in $HOME/go.

1
2
3
$HOME/go/src
$HOME/go/bin
$HOME
  1. Install golang with homebrew
1
brew install go

2 set GOPATH on unix like os.

1
2
3
cat << EOF >> ~/.zshrc
export GOPATH=$HOME/go
export PATH=$PATH:%GOPATH/bin

set GOPATH on window

1
2
setx GOPATH $USERPROFILE%\go
setx path "%PATH%;%USERPROFILE%\bin"

GO command

  • go run
  • go build

hello world

1
2
3
4
5
6
# hello.go
pachage main
import "fmt"
func main(){
	fmt.Println("Hello, world!")
}
1
2
3
4
5
6
7
8
#Just run go code,without build binary in currently directory path.but build it in temporary direcory.
go run hello.go

#Build binary without run 
go build hello.go

# Build specific binary name
go build -o hellworld hello.go

Install Third-Party Go Tools

  • Install 3rd go tools
1
go install github.com/rakyll/hey@latest
  • Upgrade 3rd go tools by run the install command again
1
2
3
4
5
6
7
8
go install github.com/rakyll/hey@latest

# Install goimports
go install golang.org/x/tools/cmd/goimports@latest
#go: downloading golang.org/x/tools v0.1.0
#go: downloading golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4
#go: downloading golang.org/x/mod v0.3.0
#go: downloading golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1

goimport

1
2
3
4
goimports -l -w .
# The `-l` flag tells `goimports` to print the files with incorrect formatting to the console
# The `-w` flag tells `goimports` to modify the files in-place.
# The . specifies the files to be scanned: everything in the current directory and all of its sub-directories.

golint

  • Install golint
1
go install golang.org/x/lint/golint@latest
  • Run it
1
2
# Run golint over your entire project
golint ./...
Built with Hugo
Theme Stack designed by Jimmy