惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
Google Developers Blog
宝玉的分享
宝玉的分享
月光博客
月光博客
B
Blog
云风的 BLOG
云风的 BLOG
Google DeepMind News
Google DeepMind News
Engineering at Meta
Engineering at Meta
aimingoo的专栏
aimingoo的专栏
N
Netflix TechBlog - Medium
博客园_首页
GbyAI
GbyAI
人人都是产品经理
人人都是产品经理
A
About on SuperTechFans
Y
Y Combinator Blog
L
LangChain Blog
有赞技术团队
有赞技术团队
D
Docker
爱范儿
爱范儿
博客园 - 司徒正美
H
Hackread – Cybersecurity News, Data Breaches, AI and More
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
酷 壳 – CoolShell
酷 壳 – CoolShell
Microsoft Security Blog
Microsoft Security Blog
D
DataBreaches.Net

Posts on WKLKEN THINKING

apisix 中的 lrucache apisix 中的服务发现机制 apisix 中的负载均衡 apisix etcd机制 聊聊框架 关于 k8s 的 zero downtime deployment 一些建议 apisix 遇到的一些问题 关于在除夕前一天换了一个洗衣机的故事 Django DRF 性能优化 DRF 的一些实践 Part1: Serializer DRF继承关系图 Better Code: 关于接口的灵活性 新的仓库: wklken/naming 缓存使用的一些经验 Better Code: 抽象: 可扩展性与可维护性的抉择 Better Code: 异常时, 该提示用户哪些信息? Better Code: 更好的异常日志打印 Go: some libs Go: go-redis/cache升级的坑 Go: logrus性能提升 Go: gin validation 远程办公的一点总结 项目管理实践: 风险驱动开发 Go: 一种error wrap调用链处理方式 漫谈技术选型 Go: 基于 apitest 做handler层单元测试 Go: go-sql-driver interpolateparams参数优化 [分享]深度工作 你需要更多的思考时间 Django项目重构小结
Go: 开发过程中的一些bug
2021-01-28 · via Posts on WKLKEN THINKING

1. make slice

很容易漏掉中间参数, 引入 bug并且很难排查

package main

import "fmt"

func doCopy(a []string) []string {
	b := make([]string, len(a))
	for _, i := range a {
		b = append(b, i)
	}
	return b
}

func main() {
	a := []string{"hello"}
	b := doCopy(a)
	fmt.Println(b, len(b))
}

得到结果

[ hello] 2

实际上

a := make([]int, 5)  // len(a)=5
a := make([]int, 0, 5)  // len(a)=0 cap(b)=5

2. shadow

本来应该使用=赋值, 错误地使用了:= (这个govet可以扫出来)

var form url.Values
if checkForm {
  form := make(url.Values)
  util.CopyValues(form, r.PostForm)
}

3. err == or !=

容易敲错, 且不好排查

if err == nil {

}

if err != nil {

}

4. 漏掉了return

在本该return的地方漏掉了, 导致逻辑继续往后走, 这种在gin的handler和middleware特别容易漏

if err != nil {
  render.JSON(w, err.Status, err)
  // 这里漏掉了 return
}

next.ServeHTTP(w, r)

5. scopelint

scopelint

package main

import "fmt"

func main() {
	values := []string{"a", "b", "c"}
	var copies []*string
	for _, val := range values {
		copies = append(copies, &val)
	}
	fmt.Println(copies)
}

此时得到

[0xc000010200 0xc000010200 0xc000010200]

预期的应该是

package main

import "fmt"

func main() {
	values := []string{"a", "b", "c"}
	var copies []*string
	for _, val := range values {
    // add := here
		val := val
		copies = append(copies, &val)
	}
	fmt.Println(copies)

}

6. error.Is放错了位置

if err != nil {
  return err
}

...


if error.Is(err, DEMOError) {
    // will never execute
}

7. close the connection

// the connection is not close
_, err := sqlx.Connect("mysql", db.dataSource)

// should be
conn, err := sqlx.Connect("mysql", db.dataSource)
if err != nil {
	return
}

conn.Close()