您的位置:首页 > 编程语言 > Go语言

golang构建函数

2017-12-25 11:56 113 查看
写代码注意事项

软件是信息流,依靠数据驱动

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. 锁的使用

非递归的锁不能重复加锁,否则会死锁
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: