当前位置:网站首页>Go----Go 语言命名规范
Go----Go 语言命名规范
2022-07-26 21:11:00 【redrose2100】
一、Go语言命名规范简介
命名规则涉及变量、常量、全局函数、结构、接口、方法等的命名。 Go语言从语法层面进行了以下限定:任何需要对外暴露的名字必须以大写字母开头,不需要对外暴露的则应该以小写字母开头。
当命名(包括常量、变量、类型、函数名、结构字段等等)以一个大写字母开头,如:GetUserName,那么使用这种形式的标识符的对象就可以被外部包的代码所使用(客户端程序需要先导入这个包),这被称为导出(像面向对象语言中的 public); 命名如果以小写字母开头,则对包外是不可见的,但是他们在整个包的内部是可见并且可用的(像面向对象语言中的 private )
二、Go 语言常见的命名规范
2.1 包名称
保持package的名字和目录保持一致,尽量采取有意义的包名,简短,有意义,尽量和标准库不要冲突。包名应该为小写单词,不要使用下划线或者混合大小写。
package user
package service
2.2 文件命名
尽量采取有意义的文件名,简短,有意义,应该为小写单词,使用下划线分隔各个单词。
first_demo.go
2.3 结构体命名
采用驼峰命名法,首字母根据访问控制大写或者小写
struct 申明和初始化格式采用多行,例如下面:
type CustomerOrder struct {
Name string
Address string
}
order := CustomerOrder{
"tom", "北京海淀"}
2.4 接口命名
命名规则基本和上面的结构体类型
单个函数的结构名以 “er” 作为后缀,例如 Reader , Writer 。
type Reader interface {
Read(p []byte) (n int, err error)
}
2.5 变量命名
和结构体类似,变量名称一般遵循驼峰法,首字母根据访问控制原则大写或者小写,但遇到特有名词时,需要遵循以下规则:
如果变量为私有,且特有名词为首个单词,则使用小写,如 appService 若变量类型为 bool 类型,则名称应以 Has, Is, Can 或 Allow 开头
var isExist bool
var hasConflict bool
var canManage bool
var allowGitHook bool
2.6 常量命名
常量均需使用全部大写字母组成,并使用下划线分词
const APP_URL = "https://www.duoke360.com"
如果是枚举类型的常量,需要先创建相应类型:
type Scheme string
const (
HTTP Scheme = "http"
HTTPS Scheme = "https"
)
2.7 错误处理
错误处理的原则就是不能丢弃任何有返回err的调用,不要使用 _ 丢弃,必须全部处理。接收到错误,要么返回err,或者使用log记录下来尽早return:一旦有错误发生,马上返回,尽量不要使用panic,除非你知道你在做什么,错误描述如果是英文必须为小写,不需要标点结尾,采用独立的错误流进行处理
// 错误写法
if err != nil {
// 错误处理
} else {
// 正常代码
}
// 正确写法
if err != nil {
// 错误处理
return // 或者继续
}
// 正常代码
2.7 单元测试
单元测试文件名命名规范为 example_test.go 测试用例的函数名称必须以 Test 开头,例如:TestExample 每个重要的函数都要首先编写测试用例,测试用例和正规代码一起提交方便进行回归测试
边栏推荐
- 三星发布1.08亿像素图像传感器ISOCELL Bright HMX,小米将首发
- Pbootcms一级栏目下的二级三级栏目高亮
- 《暑假每日一题》Week 7:7.18 - 7.24
- Flextronics responded to the "seizure of Huawei materials" incident: deeply regretted, looking forward to continuing cooperation!
- 单机部署flink,创建oracle19c rac的连接表时报错 ORA-12505,有哪位大佬帮忙
- Props with type Object/Array must...
- Leetcode exercise - Sword finger offer II 005. maximum product of word length
- If you do not add waitkey() function after imshow() function, it will not be displayed
- 吃透负载均衡
- 25 cool interactive charts, one entry plotly
猜你喜欢

(C语言)文件的基本操作

梦里的一碗面

A friend with a monthly salary of 50000 told me that you were just doing chores

Kalibr calibration realsensed435i -- multi camera calibration

JDBC总结

2022年简历石沉大海,别投了,软件测试岗位饱和了....

My SQL is OK. Why is it still so slow? MySQL locking rules

《暑假每日一题》Week 7:7.18 - 7.24

OPPO 自研大规模知识图谱及其在数智工程中的应用

七、微信小程序运行报错:Error: AppID 不合法,invalid appid
随机推荐
织梦文档关键词维护不管用
Isilon 的OneFs常见操作命令(一)
The United States once again challenged: Forbidding contractors to purchase the equipment and technology of these five Chinese companies
三星发布1.08亿像素图像传感器ISOCELL Bright HMX,小米将首发
25 cool interactive charts, one entry plotly
攻防世界----ics-07
Props with type Object/Array must...
Drag and drop table rows
Leetcode exercise - Sword finger offer II 005. maximum product of word length
按图搜索义乌购商品(拍立淘) API
Keyword maintenance of dream weaving document doesn't work
A friend with a monthly salary of 50000 told me that you were just doing chores
Pytorch torch. add() torch. add_ () usage
VB.net Chart1的处理
Technology sharing | do you know the functions of the server interface automated testing and requests library?
In depth interpretation of happens before principle
基于CAShapeLayer和贝塞尔曲线的圆形进度条动画
Six instructions of Memcache based caching mechanism
Preview image before uploading
按关键字搜索易贝商品 API