当前位置:网站首页>Go coding specification
Go coding specification
2022-06-24 21:06:00 【AcTarjan】
Go Official code specification
name
- For file names , It should always be in lowercase , Underline different words
- For variables , Hump nomenclature . Proper nouns should be in the same case , example : Sure URL or url , And not Url . Variable names should be as short as possible , Especially local variables . For some special variables and global variables , It may need more description , You can use long names
- about interface, Name should begin with er ending , Give priority to the existing interfaces in the standard library
- For structures , Note the exportability of fields , Capitalized fields can be exported
Getter: obj.Xxx()
Setter: obj.SetXxx(xxx type) - For the receiver , Should not be used me、this、self And other common names , Instead, use a short (1 or 2 Characters ) And it can reflect the naming of structure names , And the receiver name of the same structure should be consistent
- about url, Should contain only lowercase letters , example :/name/get, And not /getName
- about post Of the form in the request key, Use underline nomenclature
- about json data ,key Use underline nomenclature , The type of value is selected according to the actual situation , Instead of using the same string
Variable declarations
- For structures , The values of each field are initialized at the time of declaration
// recommend
stu := Student{
Name: "AcTarjan"
}
// Not recommended
stu := Student{
}
stu.Name = "AcTarjan"
- Declare an empty array slice
// recommend , Not for arr Allocate memory ,arr = nil
var arr []string
// Not recommended , This will help arr Allocate memory
arr := []string{
}
Receiver type
- The receiver : The caller of the method
- Omnipotent advice : If you don't know which delivery to use , Please select pointer passing !
- It is recommended to use pointer passing :
When the receiver needs to be modified inside the function , Must be passed with a pointer .
When the recipient is a structure , And it includes sync.Mutex Or similar members for synchronization . Avoid member copy .
When the receiver type is a large structure or a large array , To improve performance .
When the recipient is a structure , An array or slice, And the elements are pointers , And these elements may be modified inside the function , This makes the semantics of functions more explicit . - It is recommended to use value passing :
When the recipient is map, chan, func, Because they are reference types in themselves .
When the recipient is slice, And the function will not be internal to slice Slice or reallocate space .
When the recipient is a small structure , Small array , And there is no need to modify the elements inside , The elements inside are some basic types
Parameter passing
- In general , Passing values instead of pointers
Don't pass a pointer instead of a value to save a little space . Unless it is a huge structure to be transferred or it can be predicted that it will become very large in the future , Pointers are a good choice . - When parameters need to be modified and the caller needs to use these modifications , Pass pointer
Pointer dependent
// normal , although arr = nil, But this slice exists , namely &arr != nil
var arr []string
err := json.Unmarshal(bs,&arr)
// error ,arr Is a pointer ,arr = nil, That is, the slice does not exist
var arr *[]string
err := json.Unmarshal(bs,arr)
error message
- Error messages should be all lowercase letters
Log printing
- Only the key nodes are printed INFO Level log
- Only the caller handles error Print the log when
边栏推荐
- Leetcode(146)——LRU 缓存
- Variable setting in postman
- Dx12 engine development course progress - where does this course go
- Grating diffraction
- Pytest testing framework
- How to enhance influence
- What does virtualization mean? What technologies are included? What is the difference with private cloud?
- Selenium crawl notes
- Record a deletion bash_ Profile file
- NPM download speed is slow
猜你喜欢
![Use the transparent [x] cross button image in the dialog](/img/0c/2be7bc7f20b581a2cc745d9cabe9ff.jpg)
Use the transparent [x] cross button image in the dialog

Design of routing service for multi Activity Architecture Design

Wechat applet custom tabbar

Intermediary model -- collaboration among departments

Shrimp skin test surface treated

Learn to use a new technology quickly

Sequence stack version 1.0

Limit summary (under update)

得物多活架构设计之路由服务设计

Basic operation of sequence table
随机推荐
[performance tuning basics] performance tuning standards
Smooth live broadcast | analysis of key technologies for live broadcast pain points
Learn to use a new technology quickly
Leetcode(146)——LRU 缓存
Responsibility chain mode -- through interview
Leetcode (455) - distribute cookies
List set Introduction & common methods
What are the problems with traditional IO? Why is zero copy introduced?
Pytest test framework II
等保备案是等保测评吗?两者是什么关系?
图的基本概念以及相关定义
Common self realization functions in C language development
全上链哈希游戏dapp系统定制(方案设计)
Dongyuhui is not enough to bring goods to "rescue" live broadcast
Berkeley, MIT, Cambridge, deepmind and other industry leaders' online lectures: towards safe, reliable and controllable AI
More than ten years' work experience is recommended at the bottom of the box: how much does it cost to find a job? See here! Brothers and sisters are recommended to collect and pay attention
在Dialog中使用透明的【X】叉叉按钮图片
Sleep revolution - find the right length of rest
Shrimp skin test surface treated
Packaging_ Conversion between basic type and string type