golang构建函数
来源:互联网 发布:写五线谱的软件 编辑:程序博客网 时间:2024/06/05 15:13
写代码注意事项
软件是信息流,依靠数据驱动
1. 输入数据 参数
函数名+request方便扩展,见名知意参数合法性检查
2.输出参数
函数名+response
type CheckHitResponseData struct { Fish []*CheckHitFish `json:"fish"` Sum float64 `json:"sum"`}type CheckHitResponse struct { Code int `json:"code"` Msg string `json:"msg"` Data CheckHitResponseData `json:"data"`}
3. 返回值
err 为空正常 否则为异常(定义返回码)
func (psh *PushImpl) UserJoin(request *push.PushUserJoinRequest, response *push.PushUserJoinResponse) (err error) {return}
4. 异常的处理:
errors.go 定义各种error异常退出时 删除添加的数据、缓存、关闭数据连接、打开的文件、释放锁
5.I/O
操作的全局变量、缓存、对外提供的服务、send/receive 的message
6. 锁的使用
非递归的锁不能重复加锁,否则会死锁
阅读全文