GO語言學習筆記(一)Go語言的初步瞭解
一·、Go語言的官方網站
二、安裝Go語言
Linux版本:
tar zxf go1.11.4.linux-amd64.tar.gz mv go /usr/local/
三、設定GOPATH和GOROOT環境變數
vim /etc/profile
增加這三行:
export GOROOT=/usr/local/go export PATH=$GOROOT/bin:$PATH export GOPATH=$GOROOT/gocode
使其生效:source /etc/profile
四、驗證
執行:go version
go version go1.11.4 linux/amd64
五、Go環境變數介紹
執行:go env
GOARCH="amd64" GOBIN="" GOCACHE="/root/.cache/go-build" GOEXE="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="linux" GOOS="linux" GOPATH="/usr/local/go/gocode/" GOPROXY="" GORACE="" GOROOT="/usr/local/go" GOTMPDIR="" GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64" GCCGO="gccgo" CC="gcc" CXX="g++" CGO_ENABLED="1" GOMOD=""386、 CGO_CFLAGS="-g -O2" CGO_CPPFLAGS="" CGO_CXXFLAGS="-g -O2" CGO_FFLAGS="-g -O2" CGO_LDFLAGS="-g -O2" PKG_CONFIG="pkg-config" GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build119196690=/tmp/go-build -gno-record-gcc-switches"
GOROOT="/usr/local/go"//表示Go在伺服器上安裝位置
GOPATH="/usr/local/go/gocode/"//表示Go語言的工作目錄
GOARCH="amd64"//表示機器的處理器架構,值可以是:386、amd64、arm
GOOS="linux"//表示當前機器的作業系統,值可以是:darwin、freebsd、linux、windows(其中darwin就是mac系統)
GOBIN=""//表示編譯器和連結器的安裝位置,預設位置是 $GOROOT/bin,一般情況下你可以將它的值設定為空,Go 將會使用前面提到的預設值
六、Go語言原始碼路徑說明
[root@Go ~]# ls -l /usr/local/go total 180 drwxr-xr-x2 root root258 Dec 15 07:36 api -rw-r--r--1 root root 55284 Dec 15 07:36 AUTHORS drwxr-xr-x2 root root42 Dec 15 07:48 bin -rw-r--r--1 root root1339 Dec 15 07:36 CONTRIBUTING.md -rw-r--r--1 root root 71070 Dec 15 07:36 CONTRIBUTORS drwxr-xr-x8 root root4096 Dec 15 07:36 doc -rw-r--r--1 root root5686 Dec 15 07:36 favicon.ico drwxr-xr-x2 root root6 Jan 23 15:30 gocode drwxr-xr-x3 root root18 Dec 15 07:36 lib -rw-r--r--1 root root1479 Dec 15 07:36 LICENSE drwxr-xr-x 15 root root202 Dec 15 07:48 misc -rw-r--r--1 root root1303 Dec 15 07:36 PATENTS drwxr-xr-x9 root root199 Dec 15 07:48 pkg -rw-r--r--1 root root1607 Dec 15 07:36 README.md -rw-r--r--1 root root26 Dec 15 07:36 robots.txt drwxr-xr-x 46 root root4096 Dec 15 07:36 src drwxr-xr-x 22 root root8192 Dec 15 07:48 test -rw-r--r--1 root root8 Dec 15 07:36 VERSION
1)bin目錄
-rwxr-xr-x 1 root root 13057654 Dec 15 07:38 go -rwxr-xr-x 1 root root 16006569 Dec 15 07:48 godoc -rwxr-xr-x 1 root root3373518 Dec 15 07:38 gofmt
包含可執行檔案,如:編輯器、Go工具
2)doc目錄
drwxr-xr-x 3 root root85 Dec 15 07:36 articles -rw-r--r-- 1 root root33541 Dec 15 07:36 asm.html -rw-r--r-- 1 root root3006 Dec 15 07:36 cmd.html -rw-r--r-- 1 root root16933 Dec 15 07:36 code.html drwxr-xr-x 2 root root202 Dec 15 07:46 codewalk -rw-r--r-- 1 root root8701 Dec 15 07:36 conduct.html -rw-r--r-- 1 root root4586 Dec 15 07:36 contrib.html -rw-r--r-- 1 root root36834 Dec 15 07:36 contribute.html -rw-r--r-- 1 root root16577 Dec 15 07:36 debugging_with_gdb.html drwxr-xr-x 2 root root65 Dec 15 07:36 devel -rw-r--r-- 1 root root17573 Dec 15 07:36 diagnostics.html -rw-r--r-- 1 root root8865 Dec 15 07:36 docs.html -rw-r--r-- 1 root root1405 Dec 15 07:36 editors.html -rw-r--r-- 1 root root 116452 Dec 15 07:36 effective_go.html -rw-r--r-- 1 root root4319 Dec 15 07:36 gccgo_contribute.html -rw-r--r-- 1 root root17052 Dec 15 07:36 gccgo_install.html -rw-r--r-- 1 root root59265 Dec 15 07:36 go1.10.html -rw-r--r-- 1 root root36305 Dec 15 07:36 go1.11.html -rw-r--r-- 1 root root40411 Dec 15 07:36 go1.1.html -rw-r--r-- 1 root root35000 Dec 15 07:36 go1.2.html -rw-r--r-- 1 root root23115 Dec 15 07:36 go1.3.html -rw-r--r-- 1 root root33535 Dec 15 07:36 go1.4.html -rw-r--r-- 1 root root47800 Dec 15 07:36 go1.5.html -rw-r--r-- 1 root root37592 Dec 15 07:36 go1.6.html -rw-r--r-- 1 root root43702 Dec 15 07:36 go1.7.html -rw-r--r-- 1 root root59014 Dec 15 07:36 go1.8.html -rw-r--r-- 1 root root37538 Dec 15 07:36 go1.9.html -rw-r--r-- 1 root root7216 Dec 15 07:36 go1compat.html -rw-r--r-- 1 root root71461 Dec 15 07:36 go1.html -rw-r--r-- 1 root root89479 Dec 15 07:36 go_faq.html -rw-r--r-- 1 root root8843 Dec 15 07:36 go-logo-black.png -rw-r--r-- 1 root root9360 Dec 15 07:36 go-logo-blue.png -rw-r--r-- 1 root root21469 Dec 15 07:36 go-logo-white.png -rw-r--r-- 1 root root13497 Dec 15 07:36 go_mem.html drwxr-xr-x 3 root root4096 Dec 15 07:36 gopher -rw-r--r-- 1 root root 204825 Dec 15 07:36 go_spec.html -rw-r--r-- 1 root root3128 Dec 15 07:36 help.html -rw-r--r-- 1 root root34 Dec 15 07:36 ie.css -rw-r--r-- 1 root root9159 Dec 15 07:36 install.html -rw-r--r-- 1 root root19777 Dec 15 07:36 install-source.html drwxr-xr-x 2 root root131 Dec 15 07:36 play drwxr-xr-x 2 root root4096 Dec 15 07:36 progs -rw-r--r-- 1 root root4520 Dec 15 07:36 root.html -rw-r--r-- 1 root root8411 Dec 15 07:36 security.html -rw-r--r-- 1 root root2993 Dec 15 07:36 share.png -rw-r--r-- 1 root root360 Dec 15 07:36 tos.html
包含示例程式、程式碼工具、本地文件等
3)lib目錄
[root@Go go]# ls -l lib/ drwxr-xr-x 2 root root 59 Jan 24 17:33 time [root@Go go]# ls -l lib/time/ -rw-r--r-- 1 root root378 Dec 15 07:36 README -rwxr-xr-x 1 root root1367 Dec 15 07:36 update.bash -rw-r--r-- 1 root root 365101 Dec 15 07:36 zoneinfo.zip
包含文件模板
4)misc目錄
drwxr-xr-x2 root root64 Dec 15 07:36 android drwxr-xr-x2 root root15 Dec 15 07:36 arm -rwxr-xr-x1 root root110 Dec 15 07:36 benchcmp drwxr-xr-x 20 root root 4096 Dec 15 07:36 cgo drwxr-xr-x3 root root24 Dec 15 07:36 chrome -rw-r--r--1 root root159 Dec 15 07:36 editors drwxr-xr-x2 root root24 Dec 15 07:36 git drwxr-xr-x2 root root86 Dec 15 07:36 ios drwxr-xr-x2 root root26 Dec 15 07:36 linkcheck drwxr-xr-x3 root root150 Dec 15 07:36 nacl drwxr-xr-x2 root root23 Dec 15 07:36 sortac drwxr-xr-x4 root root35 Dec 15 07:36 swig drwxr-xr-x7 root root79 Dec 15 07:48 tour drwxr-xr-x2 root root53 Dec 15 07:36 trace drwxr-xr-x2 root root71 Dec 15 07:36 wasm
包含與支援Go編輯器有關的配置檔案以及cgo的示例
5)src目錄
-rwxr-xr-x1 root root407 Dec 15 07:36 all.bash -rw-r--r--1 root root726 Dec 15 07:36 all.bat -rwxr-xr-x1 root root385 Dec 15 07:36 all.rc -rwxr-xr-x1 root root2622 Dec 15 07:36 androidtest.bash drwxr-xr-x4 root root28 Dec 15 07:36 archive -rwxr-xr-x1 root root3738 Dec 15 07:36 bootstrap.bash drwxr-xr-x2 root root123 Dec 15 07:36 bufio -rwxr-xr-x1 root root1818 Dec 15 07:36 buildall.bash drwxr-xr-x2 root root24 Dec 15 07:36 builtin drwxr-xr-x2 root root234 Dec 15 07:36 bytes -rwxr-xr-x1 root root521 Dec 15 07:36 clean.bash -rw-r--r--1 root root565 Dec 15 07:36 clean.bat -rwxr-xr-x1 root root380 Dec 15 07:36 clean.rc drwxr-xr-x 24 root root289 Dec 15 07:36 cmd -rw-r--r--1 root root1519 Dec 15 07:36 cmp.bash drwxr-xr-x8 root root83 Dec 15 07:36 compress drwxr-xr-x5 root root42 Dec 15 07:36 container drwxr-xr-x2 root root131 Dec 15 07:36 context drwxr-xr-x 20 root root275 Dec 15 07:36 crypto drwxr-xr-x3 root root17 Dec 15 07:36 database drwxr-xr-x8 root root82 Dec 15 07:36 debug drwxr-xr-x 13 root root161 Dec 15 07:36 encoding drwxr-xr-x2 root root68 Dec 15 07:36 errors drwxr-xr-x2 root root45 Dec 15 07:36 expvar drwxr-xr-x2 root root115 Dec 15 07:36 flag drwxr-xr-x2 root root176 Dec 15 07:36 fmt drwxr-xr-x 14 root root173 Dec 15 07:36 go drwxr-xr-x6 root root201 Dec 15 07:36 hash drwxr-xr-x3 root root151 Dec 15 07:36 html drwxr-xr-x9 root root4096 Dec 15 07:36 image drwxr-xr-x3 root root33 Dec 15 07:36 index drwxr-xr-x 12 root root194 Dec 15 07:36 internal drwxr-xr-x3 root root146 Dec 15 07:36 io -rwxr-xr-x1 root root2116 Dec 15 07:36 iostest.bash drwxr-xr-x3 root root76 Dec 15 07:36 log -rwxr-xr-x1 root root6339 Dec 15 07:36 make.bash -rw-r--r--1 root root3946 Dec 15 07:36 make.bat -rw-r--r--1 root root553 Dec 15 07:36 Make.dist -rwxr-xr-x1 root root3136 Dec 15 07:36 make.rc drwxr-xr-x6 root root8192 Dec 15 07:36 math drwxr-xr-x5 root root4096 Dec 15 07:36 mime -rwxr-xr-x1 root root1255 Dec 15 07:36 naclmake.bash -rwxr-xr-x1 root root1216 Dec 15 07:36 nacltest.bash drwxr-xr-x 10 root root8192 Dec 15 07:36 net drwxr-xr-x5 root root4096 Dec 15 07:39 os drwxr-xr-x3 root root117 Dec 15 07:36 path drwxr-xr-x2 root root70 Dec 15 07:36 plugin -rwxr-xr-x1 root root976 Dec 15 07:36 race.bash -rw-r--r--1 root root1037 Dec 15 07:36 race.bat drwxr-xr-x2 root root4096 Dec 15 07:36 reflect drwxr-xr-x4 root root232 Dec 15 07:36 regexp -rwxr-xr-x1 root root1681 Dec 15 07:36 run.bash -rw-r--r--1 root root1147 Dec 15 07:36 run.bat -rwxr-xr-x1 root root418 Dec 15 07:36 run.rc drwxr-xr-x 10 root root 16384 Dec 15 07:36 runtime drwxr-xr-x2 root root334 Dec 15 07:36 sort drwxr-xr-x3 root root4096 Dec 15 07:36 strconv drwxr-xr-x2 root root333 Dec 15 07:36 strings drwxr-xr-x3 root root4096 Dec 15 07:36 sync drwxr-xr-x3 root root 12288 Dec 15 07:36 syscall drwxr-xr-x5 root root335 Dec 15 07:36 testing drwxr-xr-x5 root root54 Dec 15 07:36 text drwxr-xr-x2 root root4096 Dec 15 07:36 time drwxr-xr-x4 root root252 Dec 15 07:36 unicode drwxr-xr-x2 root root23 Dec 15 07:36 unsafe drwxr-xr-x3 root root24 Dec 15 07:36 vendor
包含原始碼構建指令碼和標準庫的包的完整原始碼
6)src/cmd目錄
drwxr-xr-x2 root root46 Dec 15 07:36 addr2line drwxr-xr-x3 root root73 Dec 15 07:36 api drwxr-xr-x3 root root51 Dec 15 07:36 asm drwxr-xr-x2 root root38 Dec 15 07:36 buildid drwxr-xr-x2 root root130 Dec 15 07:36 cgo drwxr-xr-x3 root root87 Dec 15 07:36 compile drwxr-xr-x3 root root121 Dec 15 07:41 cover drwxr-xr-x2 root root 4096 Dec 15 07:38 dist drwxr-xr-x3 root root85 Dec 15 07:36 doc drwxr-xr-x2 root root 4096 Dec 15 07:36 fix drwxr-xr-x4 root root258 Dec 15 07:43 go drwxr-xr-x3 root root149 Dec 15 07:36 gofmt drwxr-xr-x 15 root root178 Dec 15 07:36 internal drwxr-xr-x3 root root115 Dec 15 07:36 link drwxr-xr-x2 root root73 Dec 15 07:36 nm drwxr-xr-x3 root root60 Dec 15 07:36 objdump drwxr-xr-x2 root root55 Dec 15 07:36 pack drwxr-xr-x2 root root71 Dec 15 07:36 pprof drwxr-xr-x2 root root21 Dec 15 07:36 test2json drwxr-xr-x2 root root184 Dec 15 07:36 trace drwxr-xr-x4 root root95 Dec 15 07:36 vendor drwxr-xr-x5 root root 4096 Dec 15 07:41 vet
包含Go和C的編輯器和命令列指令碼