Go 常见错误¶
100 Go Mistakes and How to Avoid Them¶
《100 Go Mistakes and How to Avoid Them》这本书的一些重要内容笔记。
2 Code and project organization¶
2.1 #1: Unintended variable shadowing¶
varclient*http.Client// 以下代码运行后,client 依然还是 niliftracing{// Declares a client variableclient,err:=createClientWithTracing()// 新创建的同名变量覆盖外层同名的 clientiferr!=nil{returnerr}log.Println(client)}else{client,err:=createDefaultClient()iferr!=nil{returnerr}log.Println(client)}// 解决方式:不要使用 := 赋值一个新变量,需要声明一下 client 和 errvarclient*http.Clientvarerrerror// Declares an err variableiftracing{client,err=createClientWithTracing()iferr!=nil{returnerr}}else{// Same logic}
2.5 #5: Interface pollution¶
When to use interfaces:
Common behavior
Decoupling
Restricting behavior