這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
一.Golang的安裝
1.https://dl.gocn.io/ (國內下載地址)
2.https://golang.org/dl/ (國外下載地址)
3.現在studygolang中文網也可以了https://studygolang.com/dl
下載版本:
mac darwin-adm64.tar.gz
linux amd64.tar.gz
windows amd64.msi
4.window編輯器
- atom配合go-plus外掛程式
- sublime配合gosublime外掛程式;
- emacs + spacemacs配置(相對來說比較麻煩,也是mac的一個不錯的選擇.)
- Goland JetBrains
直接安裝的效果:
Windows推薦goland
二 、Ubuntu安裝
apt install golang-goroot@greg:# go envGOARCH="amd64"GOBIN=""GOEXE=""GOHOSTARCH="amd64"GOHOSTOS="linux"GOOS="linux"GOPATH=""GORACE=""GOROOT="/usr/lib/go-1.7"GOTOOLDIR="/usr/lib/go-1.7/pkg/tool/linux_amd64"CC="gcc"GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build765188684=/tmp/go-build -gno-record-gcc-switches"CXX="g++"CGO_ENABLED="1"
預設安裝了1.7,可以自訂更新到最新版1.9
root@greg:/usr/local/src/go1.9.2/src# ./make.bash##### Building Go bootstrap tool.cmd/distERROR: Cannot find /root/go1.4/bin/go.Set $GOROOT_BOOTSTRAP to a working Go tree >= Go 1.4.
報錯了
解決:export GOROOT_BOOTSTRAP=/usr/lib/go-1.7
初始化環境 GOROOT放置go的標準庫和工具鏈 $HOME/local/go (linux,mac) c:\local\go (windows) GOPATH放置第三方代碼和自己的工程 $HOME/go(linux,mac) c:\go(windows) PATH export PATH=$GOROOT/bin:$GOPATH/bin:$PATH vim ~/.bashrc export GOROOT=$HOME/local/go export GOPATH=$HOME/go export PATH=$GOROOT/bin:$GOPATH/bin:$PATH 我的Ubuntu是這樣的 GOPATH="/go" GOROOT="/usr/local/src/go" export PATH=$GOROOT/bin:$GOPATH/bin:$PATH
三、golang特性
1. 天然並發
Go語言引入了goroutine概念,它使得並發編程變得非常簡單。通過使用goroutine而不是裸用作業系統的並發機制,以及使用訊息傳遞來共用記憶體而不是使用共用記憶體來通訊, Go語言讓並發編程變得更加輕盈和安全。
通過在函數調用前使用關鍵字go,我們即可讓該函數以goroutine方式執行。 goroutine是一種比線程更加輕盈、更省資源的協程。 Go語言通過系統的線程來多路派遣這些函數的執行,使得每個用go關鍵字執行的函數可以運行成為一個單位協程。當一個協程阻塞的時候,調度器就會自動把其他協程安排到另外的線程中去執行,從而實現了程式無等待並行化運行。而且調度的開銷非常小,一顆CPU調度的規模不下於每秒百萬次,這使得我們能夠建立大量的goroutine,從而可以很輕鬆地編寫高並發序,達到我們想要的目的。
Go語言實現了CSP(通訊順序進程, Communicating Sequential Process)模型來作為goroutine間的推薦通訊方式。在CSP模型中,一個並發系統由若干並行啟動並執行順序進程組成,每個進程不能對其他進程的變數賦值。進程之間只能通過一對通訊原語實現協作。
Go語言用channel(通道)這個概念來輕巧地實現了CSP模型。 channel的使用方式比較接近Unix系統中的管道(pipe)概念,可以方便地進行跨goroutine的通訊。
在單核時代:一個線程就能把CPU跑滿,沒必要多個線程
多核:記憶體操作、io操作,可以多線程,多進程的流暢執行
Nginx:多進程架構
redis:單進程單線程,單進程只能跑滿一個cpu,目前伺服器大部分多於8核,redis一個機器上跑個6/7個,榨乾cpu
go 天然支援並發,跑一個進程就能使用7/8個核
C++和Java線程是重量級線程,高並發-->線程池,
純記憶體:8核8線程
go降低研發成本
goroute,輕量級線程,建立成千上萬個goroute成為可能
package mainimport( "time" "fmt")func test_goroute(a int) { fmt.Println(a)}func main() { for i := 0; i < 100; i++ { go test_goroute(i) } time.Sleep(time.Second)}
2.記憶體回收
記憶體自動回收,不需要開發人員管理記憶體
開發人員專註業務實現,降低了心智負擔
只需要new分配記憶體,不需要釋放
因為記憶體回收功能的支援,開發人員無需擔心所指向的對象失效的問題,因此Go語言中不需要delete關鍵字,也不需要free()方法來明確釋放記憶體。例如,對於以上的這個C語言例子,如果使用Go語言實現,我們就完全不用考慮何時需要釋放之前分配的記憶體的問題,系統會自動幫我們判斷,並在合適的時候(比如CPU相對閒置時候)進行自動垃圾收集工作。
3. channel
管道,類似unix/linux中的pipe
多個goroute之間通過channel進行通訊
支援任何類型
多傳回值,一個函數返回多個值
package mainimport( "fmt")func Add(a int, b int) int { return a + b}func Sub(a int, b int) int { return a - b}func cal(a int, b int)(int,int) { sum := a + b avg := (a+b)/2 return sum, avg}func main() { sum := Add(100, 300) sub := Sub(100, 300) fmt.Println(cal(100,200)) fmt.Println("sum=",sum) fmt.Println("sub=", sub)}
四.hello golang程式
1.任何一個代碼檔案隸屬於一個包
2.import 關鍵字,引用其他包:import(“fmt”)
3.golang可執行程式,package main,並且有且只有一個main入口函數
4. 包中函數調用:
a. 同一個包中函數,直接調用
b. 不同包中函數,通過包名+點+函數名進行調用
5. 包存取控制規則:
大寫意味著這個函數/變數是可匯出的
小寫意味著這個函數/變數是私人的,包外部不能訪問
package mainimport "fmt"func main(){ fmt.Println("hello golang")}
五.運行
1.編譯運行
go build hello.go ll -rwxr-xr-x 1 greg greg 1859967 12月 23 21:29 hello* -rw-r--r-- 1 greg greg 75 12月 23 21:25 hello.go greg@greg:~/go$ ./hello hello golang
跟go沒有關係了,脫離了go
greg@greg:~/go$ file hellohello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped
2.go run hello.go
3.各個版本的編譯
把mac編程Linux程式 file hello.lua go build hello.go GOOS=linux go build hello.go GOOS=windows go build hello.go GOOS=darwin go build hello.go GOOS=linux go build -o hello.linux hello.goGOOS=windows go build -o hello.exe hello.goGOOS=darwin go build -o hello.mac hello.gogreg@greg:~/go$ file hello.mac hello.mac: Mach-O 64-bit x86_64 executable, flags:<NOUNDEFS>greg@greg:~/go$ ./hello.linux hello golanggreg@greg:~/go$ file hello.exe hello.exe: PE32+ executable (console) x86-64 (stripped to external PDB), for MS Windowsset GOOS=windowsgo build hello.go
六、go常用工具
gofmt -w hello.go代碼完美
goimports -w hello.go 沒有包就加上,有多餘的包就刪除
一鍵編譯go build
go build github.com/greg1617/ningxin
一鍵測試:go test
go test github.com/greg1617/ningxin
一鍵下載更新依賴並編譯go get
go get github.com/greg1617/ningxin
自動文檔工具godoc
godoc -http=:9090
線上查看文檔
godoc.org/github.com/golang/protobuf/proto