golang notes (1)

1, how to install Go

download binary distribution(latest is 1.14.3): https://golang.google.cn/dl/

$sudo tar -C /usr/local -xzf go1.14.3.linux-amd64.tar.gz

$vim ~/.profile
export PATH=$PATH:/usr/local/go/bin

$source ~/.profile

$go version
go version go1.14.3 linux/amd64

2, set GOPROXY

$go build hello.go
go: finding module for package github.com/google/go-cmp/cmp
hello.go:6:5: module github.com/google/go-cmp/cmp: Get "https://proxy.golang.org/github.com/google/go-cmp/cmp/@v/list": dial tcp 172.217.160.81:443: i/o timeout

$go env | grep proxy
GOPROXY="https://proxy.golang.org,direct"

$go env -w GO111MODULE=on

$go env -w GOPROXY=https://goproxy.io,direct

3, play with hello.go, morestrings.go and morestrings_test.go

follow https://golang.google.cn/doc/code.html

(20:43 dabs@CNU1343VF8 morestrings) > ls
morestrings_test.go  reverse.go

(20:46 dabs@CNU1343VF8 morestrings) > pwd
/home/dabs/dev/golang/hello/morestrings

(20:46 dabs@CNU1343VF8 morestrings) > ls
morestrings_test.go  reverse.go

(20:46 dabs@CNU1343VF8 morestrings) > cd ..

(20:46 dabs@CNU1343VF8 hello) > pwd
/home/dabs/dev/golang/hello

(20:46 dabs@CNU1343VF8 hello) > ls
go.mod  go.sum  hello  hello.go  morestrings

(20:46 dabs@CNU1343VF8 hello) > ./hello
Hello ,Go!

  string(
- 	"Hello World",
+ 	"Hello Go",
  )

(20:47 dabs@CNU1343VF8 hello) > cd morestrings/ && go test
PASS
ok  	zhenggao2/hello/morestrings	0.003s

Follow https://segmentfault.com/a/1190000021425527 for the magic go.sum file.

4, get 'Tour of Go' and begin the journey into Go

$go get golang.org/x/tour

$~/go/bin/tour

In case you have the 'Couldn't find tour files: could not find go-tour content; check $GOROOT and $GOPATH' problem when running tour, here is one solution:

(00:04 dabs@CNU1343VF8 ~) > ~/go/bin/tour 
2020/05/22 00:05:00 Couldn't find tour files: could not find go-tour content; check $GOROOT and $GOPATH

(00:05 dabs@CNU1343VF8 ~) > cd ~/go/pkg/mod/golang.org/x/tour\@v0.0.0-20200508155540-0608babe047d/

(00:05 dabs@CNU1343VF8 [email protected]) > ~/go/bin/tour
2020/05/22 00:05:35 Serving content from /home/dabs/go/pkg/mod/golang.org/x/[email protected]
2020/05/22 00:05:36 A browser window should open. If not, please visit http://127.0.0.1:3999
2020/05/22 00:05:37 accepting connection from: 127.0.0.1:39640

猜你喜欢

转载自blog.csdn.net/jeffyko/article/details/106267085
今日推荐