一些知识点
- 静态类型但有类动态的特性
- 相比Java:打包和运行方式不同,Go编译打包为二进制依赖操作系统不需要运行时环境,性能上相比JIT基于目前的测试没有优劣
- Goroutine和Channel: Com-municating Sequential Processes (CSP)
Goroutines—A goroutine is a function that runs independently of the functionthat started it. Sometimes Go developers explain a goroutine as a function thatruns as if it were on its own thread.
Channels—A channel is a pipeline for sending and receiving data. Think of it asa socket that runs inside your program. Channels provide a way for one gorou-tine to send structured data to another.
- 与Java thread的区别?http://tleyden.github.io/blog/2014/10/30/goroutines-vs-threads/
Here are some of the advantages of Goroutines over threads:
You can run more goroutines on a typical system than you can threads.
Goroutines have growable segmented stacks.
Goroutines have a faster startup time than threads.
Goroutines come with built-in primitives to communicate safely between themselves (channels).
Goroutines allow you to avoid having to resort to mutex locking when sharing data structures.
Goroutines are multiplexed onto a small number of OS threads, rather than a 1:1 mapping.
You can write massively concurrent servers withouth having to resort to evented programming.
- WaitGroup和Lock
- defer和GC
- *和&
- package/import/func
- variable和assignment
- error和panic
An error indicates that a particular task couldn’tbe completed successfully.
A panic indicates that a severe event occurred, probablyas a result of a programmer error.
Tips
- 通过brew安装,如何设置GOPATH?https://gist.github.com/vsouza/77e6b20520d07652ed7d
.zshrc
export GOPATH=$HOME/golang
export GOROOT=/usr/local/opt/go/libexec
export PATH=$PATH:$GOPATH/bin
export PATH=$PATH:$GOROOT/bin
- 网络原因导致go get不工作:
下载tools的github repo:https://github.com/golang/tools
改目录为golang.org/x/tools,并copy到/usr/local/Cellar/go/1.6.3/libexec/src
运行go install golang.org/x/tools/cmd/goimports
成功的话能找到GOPATH/bin/goimports
如何下载包?例如:go get github.com/kylelemons/go-gypsy/yaml,在GOPATH/src下面能发现下载好的package
Intellij里面可以增加Go插件的Global libraries路径$GOPATH
References
- 《Go in Practices》
- 对 Go 语言的综合评价
- Effective Go
- Coding Style