Development tool: GoLand
The official code specification: https://golang.org/doc/effective_go.html
1. The tool
- Use the FMT tool to format the code before submitting it
- Static checks are performed using vet tools before code submission
2. Directory specifications
| – bin
| – build
| – build_dev/test/release. Sh
| – gen – go
| config –
| – dev/test/release. Yml
| – PKG
The controller | –
| – dao
| – RPC
| – service
| – * _impl. Go
| – * _test. Go
| – * _mock. Go
| – vendor
| – README, md
3. Naming conventions
- file
- Lowercase + underscore
- Package name
- The package name must be the same as the directory to avoid conflicts with the standard library
- Avoid importing relative paths
- Method/interface
- Use the hump nomenclature
- For a non-external method, the first letter must be lowercase
- variable
- Use the hump nomenclature
- constant
- Uppercase + underscore
4. Comment
- Can be achieved by
/ *... * /
or/ /...
Add comments,//
You should add a space after that - The comment content needs to be above the file/method/variable
Abnormal 5.
- Exceptions need to be judged and processed
- Do not assign error to anonymous variables
_
6. Other
- Disallow Panic calls in logic, select log.fatal of logs
- Don’t call defer too often
- Return as early as possible and as soon as an error occurs
-
If accepts an initialization statement, and the convention is to create a local variable as follows
iferr := file.Chmod(0664); err ! = nil {return err }Copy the code
-
The receiver name of the method is usually the first letter of strcut and lowercase, not this, me, or self
type rpcClient struct {
once sync.Once
}
func (r *rpcClient) newCodec(contentType string) (codec.NewCodec, error) {
//
}
- For a variable of type bool
var b bool
Instead of comparing it to true/false, use it as a judgment directly - Byte /string Slice equality comparison, using Equal
- Do not use pointer passing when recipients are map, chan, func, as they are themselves reference types
- Do not use pointer passing when the receiver is a slice and the slice is not sliced or reallocated internally
- When a function needs to modify the receiver internally, it must use pointer passing
- When the receiver is a structure and contains
sync.Mutex
Or a similar member for synchronization. You must use pointer passing to avoid member copying - When the receiver type is a structure and large, or a large array, pointer passing is recommended to improve performance, and value passing is fine for other scenarios