Golang learning resources collect

Learning:

【 go语言教程 】http://c.biancheng.net/golang/  该教程结合着c、java、c++的对比进行知识点的介绍

【 Effective GO 】https://go101.org/article/channel-closing.html 一些注意点

50 Shades of Go: Traps, Gotchas, and Common Mistakes for New Golang Devshttp://devs.cloudimmunity.com/gotchas-and-common-mistakes-in-go-golang/index.html  一些初级建议,total beginner部分基本编译器会检查

Uber Go 语言代码风格指南https://mp.weixin.qq.com/s?__biz=MjM5OTcxMzE0MQ==&mid=2653373082&idx=1&sn=d92f50813fe165a4440c6a7e0674c73a&chksm=bce4a2808b932b969467a081172afd6a9cd421fdfed94d487602bd6a6b264a25a23c2c706f28&mpshare=1&scene=1&srcid=&sharer_sharetime=1571032253723&sharer_shareid=d4a0479095676aa066b5033707d32ae7&rd2werd=1#wechat_redirect

推荐书籍:Go 语言圣经 : 作为go的入门教程

Advance:

【 Go语言内幕 】 http://blog.jobbole.com/90574/ 比较枯燥,但是比较深入,从go语言的构建说起,编译、链接、运行时、内存分配

深入理解gohttps://tiancaiamao.gitbooks.io/go-internals/content/zh/ 比较全面的深入剖析了go的机制, P、G、M调度,GC、cgo等

Go语言高级教程https://chai2010.cn/advanced-go-programming-book/ 深入了go的实现,更为全面系统

Error:

错误字符串由于相对固定,一般在包作用域声明,应尽量减少在使用时直接使用 errors.New 返回。

https://blog.golang.org/error-handling-and-go

Interface:

【 理解go interface 5个关键点https://sanyuesha.com/2017/07/22/how-to-understand-go-interface/

【 about assign interface slice []interface{}https://github.com/golang/go/wiki/InterfaceSlice

Channel:

【 How to gracefully close channelhttps://go101.org/article/channel-closing.html

One general principle of using Go channels is don’t close a channel from the receiver side and don’t close a channel if the channel has multiple concurrent senders.

It can be garbage collceted, so usually only close when it be used to broadcast, and only sender side

HttpResponseBody:

https://hackernoon.com/avoiding-memory-leak-in-golang-api-1843ef45fca8

Http Response body is very easy to cause memory leak:

1: first check whether response body is nil, if not nil, defer close

2: then to check if response with error, if so return

3: after upon two steps, if didn’t use the content in body, read it before close , other it will also cause time_wait connections raise

http://tleyden.github.io/blog/2016/11/21/tuning-the-go-http-client-library-for-load-testing/

(Above link also tells to set MaxIdleConnsPerHost when necessary to avoid time_wait connections to many)

Right thing to do:

1、resp.Body should be closed before error judge

2、resp.Body should be read even it’s useless

Slice:

when parameter passed in is a slice or map, and its size will be changed [add / delete], the parameter should be passed by pointer,
otherwise, once the operation trigger the capacity enlarge or decrease, it will cause reference failed which lead to panic , it’s dangerous.
We should be careful enough to handle with some user defined type, it’s usually not that obvious to see its real type.

array:

for range方式迭代的性能可能会更好一些,因为这种迭代可以保证不会出现数组越界的情形,每轮迭代对数组元素的访问时可以省去对下标越界的判断。
for range方式迭代,还可以忽略迭代时的下标:


其中times对应一个[5][0]int类型的数组,虽然第一维数组有长度,但是数组的元素[0]int大小是0,因此整个数组占用的内存大小依然是0。没有付出额外的内存代价,我们就通过for range方式实现了times次快速迭代。

channel:


在这里,我们并不关心管道中传输数据的真实类型,其中管道接收和发送操作只是用于消息的同步。对于这种场景,我们用空数组来作为管道类型可以减少管道元素赋值时的开销。当然一般更倾向于用无类型的匿名结构体代替:



stack:

Go1.4之后改用连续的动态栈实现,也就是采用一个类似动态数组的结构来表示栈。不过连续动态栈也带来了新的问题:当连续栈动态增长时,需要将之前的数据移动到新的内存空间,这会导致之前栈中全部变量的地址发生变化。虽然Go语言运行时会自动更新引用了地址变化的栈变量的指针,但最重要的一点是要明白Go语言中指针不再是固定不变的了(因此不能随意将指针保持到数值变量中,Go语言的地址也不能随意保存到不在GC控制的环境中,因此使用CGO时不能在C语言中长期持有Go语言对象的地址)。

 

0

发表评论