一.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-go root@greg:# go env GOARCH="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/dist ERROR: 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 main import( "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 main import( "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 main import "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 hello hello: 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.go GOOS=windows go build -o hello.exe hello.go GOOS=darwin go build -o hello.mac hello.go greg@greg:~/go$ file hello.mac hello.mac: Mach-O 64-bit x86_64 executable, flags:<NOUNDEFS> greg@greg:~/go$ ./hello.linux hello golang greg@greg:~/go$ file hello.exe hello.exe: PE32+ executable (console) x86-64 (stripped to external PDB), for MS Windows set GOOS=windows go 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
