1.https://dl.gocn.io/ (國內下載地址)linux
2.https://golang.org/dl/ (國外下載地址)git
3.如今studygolang中文網也能夠了https://studygolang.com/dlgithub
下載版本:golang
mac darwin-adm64.tar.gz
linux amd64.tar.gz
windows amd64.msiredis
4.window編輯器 編程
直接安裝的效果:bootstrap
Windows推薦goland vim
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.9c#
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.
報錯了windows
解決: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
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) }
內存自動回收,不須要開發人員管理內存
開發人員專一業務實現,下降了心智負擔
只須要new分配內存,不須要釋放
由於垃圾回收功能的支持,開發者無需擔憂所指向的對象失效的問題,所以Go語言中不須要delete關鍵字,也不須要free()方法來明確釋放內存。例如,對於以上的這個C語言例子,若是使用Go語言實現,咱們就徹底不用考慮什麼時候須要釋放以前分配的內存的問題,系統會自動幫咱們判斷,並在合適的時候(好比CPU相對空閒的時候)進行自動垃圾收集工做。
管道,相似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) }
1.任何一個代碼文件隸屬於一個包
2.import 關鍵字,引用其餘包:import(「fmt」)
3.golang可執行程序,package main,而且有且只有一個main入口函數
4. 包中函數調用:
a. 同一個包中函數,直接調用
b. 不一樣包中函數,經過包名+點+函數名進行調用
5. 包訪問控制規則:
大寫意味着這個函數/變量是可導出的
小寫意味着這個函數/變量是私有的,包外部不能訪問
package main import "fmt" func main(){ fmt.Println("hello golang") }
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
把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
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