You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
903 lines
24 KiB
903 lines
24 KiB
2 years ago
|
## OpenIM development specification
|
||
|
We have very high standards for code style and specification, and we want our products to be polished and perfect
|
||
2 years ago
|
|
||
2 years ago
|
## 1. Code style
|
||
2 years ago
|
|
||
2 years ago
|
### 1.1 Code format
|
||
2 years ago
|
|
||
2 years ago
|
- Code must be formatted with `gofmt`.
|
||
|
- Leave spaces between operators and operands.
|
||
|
- It is recommended that a line of code does not exceed 120 characters. If the part exceeds, please use an appropriate line break method. But there are also some exception scenarios, such as import lines, code automatically generated by tools, and struct fields with tags.
|
||
|
- The file length cannot exceed 800 lines.
|
||
|
- Function length cannot exceed 80 lines.
|
||
|
- import specification
|
||
|
- All code must be formatted with `goimports` (it is recommended to set the code Go code editor to: run `goimports` on save).
|
||
|
- Do not use relative paths to import packages, such as `import ../util/net`.
|
||
|
- Import aliases must be used when the package name does not match the last directory name of the import path, or when multiple identical package names conflict.
|
||
2 years ago
|
|
||
|
```go
|
||
2 years ago
|
// bad
|
||
|
"github.com/dgrijalva/jwt-go/v4"
|
||
2 years ago
|
|
||
2 years ago
|
//good
|
||
|
jwt "github.com/dgrijalva/jwt-go/v4"
|
||
2 years ago
|
```
|
||
2 years ago
|
- Imported packages are suggested to be grouped, and anonymous package references use a new group, and anonymous package references are explained.
|
||
2 years ago
|
|
||
|
```go
|
||
2 years ago
|
import (
|
||
|
// go standard package
|
||
|
"fmt"
|
||
|
|
||
|
// third party package
|
||
|
"github.com/jinzhu/gorm"
|
||
|
"github.com/spf13/cobra"
|
||
|
"github.com/spf13/viper"
|
||
|
|
||
|
// Anonymous packages are grouped separately, and anonymous package references are explained
|
||
|
// import mysql driver
|
||
|
_ "github.com/jinzhu/gorm/dialects/mysql"
|
||
|
|
||
|
// inner package
|
||
|
)
|
||
2 years ago
|
```
|
||
|
|
||
2 years ago
|
### 1.2 Declaration, initialization and definition
|
||
2 years ago
|
|
||
2 years ago
|
When multiple variables need to be used in a function, the `var` declaration can be used at the beginning of the function. Declaration outside the function must use `var`, do not use `:=`, it is easy to step on the scope of the variable.
|
||
2 years ago
|
|
||
|
```go
|
||
|
var (
|
||
2 years ago
|
Width int
|
||
|
Height int
|
||
2 years ago
|
)
|
||
|
```
|
||
|
|
||
2 years ago
|
- When initializing a structure reference, please use `&T{}` instead of `new(T)` to make it consistent with structure initialization.
|
||
2 years ago
|
|
||
|
```go
|
||
|
// bad
|
||
|
sptr := new(T)
|
||
|
sptr.Name = "bar"
|
||
|
|
||
|
// good
|
||
|
sptr := &T{Name: "bar"}
|
||
|
```
|
||
|
|
||
2 years ago
|
- The struct declaration and initialization format takes multiple lines and is defined as follows.
|
||
2 years ago
|
|
||
|
```go
|
||
|
type User struct{
|
||
2 years ago
|
Username string
|
||
|
Email string
|
||
2 years ago
|
}
|
||
|
|
||
|
user := User{
|
||
2 years ago
|
Username: "belm",
|
||
|
Email: "nosbelm@qq.com",
|
||
2 years ago
|
}
|
||
|
```
|
||
|
|
||
2 years ago
|
- Similar declarations are grouped together, and the same applies to constant, variable, and type declarations.
|
||
2 years ago
|
|
||
|
```go
|
||
|
// bad
|
||
|
import "a"
|
||
|
import "b"
|
||
|
|
||
2 years ago
|
//good
|
||
2 years ago
|
import (
|
||
2 years ago
|
"a"
|
||
|
"b"
|
||
2 years ago
|
)
|
||
|
```
|
||
|
|
||
2 years ago
|
- Specify container capacity where possible to pre-allocate memory for the container, for example:
|
||
2 years ago
|
|
||
|
```go
|
||
|
v := make(map[int]string, 4)
|
||
|
v := make([]string, 0, 4)
|
||
|
```
|
||
|
|
||
2 years ago
|
- At the top level, use the standard var keyword. Do not specify a type unless it is different from the type of the expression.
|
||
2 years ago
|
|
||
|
```go
|
||
|
// bad
|
||
1 year ago
|
var s string = F()
|
||
2 years ago
|
|
||
|
func F() string { return "A" }
|
||
|
|
||
|
// good
|
||
1 year ago
|
var s = F()
|
||
2 years ago
|
// Since F already explicitly returns a string type, we don't need to explicitly specify the type of _s
|
||
|
// still of that type
|
||
2 years ago
|
|
||
|
func F() string { return "A" }
|
||
|
```
|
||
|
|
||
2 years ago
|
- Use `_` as a prefix for unexported top-level constants and variables.
|
||
2 years ago
|
|
||
|
```go
|
||
|
// bad
|
||
|
const (
|
||
2 years ago
|
defaultHost = "127.0.0.1"
|
||
|
defaultPort = 8080
|
||
2 years ago
|
)
|
||
|
|
||
|
// good
|
||
|
const (
|
||
2 years ago
|
_defaultHost = "127.0.0.1"
|
||
|
_defaultPort = 8080
|
||
2 years ago
|
)
|
||
|
```
|
||
|
|
||
2 years ago
|
- Embedded types (such as mutexes) should be at the top of the field list within the struct, and there must be a blank line separating embedded fields from regular fields.
|
||
2 years ago
|
|
||
|
```go
|
||
|
// bad
|
||
|
type Client struct {
|
||
2 years ago
|
version int
|
||
|
http.Client
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
//good
|
||
2 years ago
|
type Client struct {
|
||
2 years ago
|
http.Client
|
||
2 years ago
|
|
||
2 years ago
|
version int
|
||
2 years ago
|
}
|
||
|
```
|
||
|
|
||
2 years ago
|
### 1.3 Error Handling
|
||
2 years ago
|
|
||
2 years ago
|
- `error` is returned as the value of the function, `error` must be handled, or the return value assigned to explicitly ignore. For `defer xx.Close()`, there is no need to explicitly handle it.
|
||
2 years ago
|
|
||
|
```go
|
||
|
func load() error {
|
||
2 years ago
|
// normal code
|
||
2 years ago
|
}
|
||
|
|
||
|
// bad
|
||
|
load()
|
||
|
|
||
2 years ago
|
//good
|
||
|
_ = load()
|
||
2 years ago
|
```
|
||
|
|
||
2 years ago
|
- When `error` is returned as the value of a function and there are multiple return values, `error` must be the last parameter.
|
||
2 years ago
|
|
||
|
```go
|
||
|
// bad
|
||
|
func load() (error, int) {
|
||
2 years ago
|
// normal code
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
//good
|
||
2 years ago
|
func load() (int, error) {
|
||
2 years ago
|
// normal code
|
||
2 years ago
|
}
|
||
|
```
|
||
|
|
||
2 years ago
|
- Perform error handling as early as possible and return as early as possible to reduce nesting.
|
||
2 years ago
|
|
||
|
```go
|
||
|
// bad
|
||
|
if err != nil {
|
||
2 years ago
|
// error code
|
||
2 years ago
|
} else {
|
||
2 years ago
|
// normal code
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
//good
|
||
2 years ago
|
if err != nil {
|
||
2 years ago
|
// error handling
|
||
|
return err
|
||
2 years ago
|
}
|
||
|
// normal code
|
||
|
```
|
||
|
|
||
2 years ago
|
- If you need to use the result of the function call outside if, you should use the following method.
|
||
2 years ago
|
|
||
|
```go
|
||
|
// bad
|
||
|
if v, err := foo(); err != nil {
|
||
2 years ago
|
// error handling
|
||
2 years ago
|
}
|
||
|
|
||
|
// good
|
||
|
v, err := foo()
|
||
|
if err != nil {
|
||
2 years ago
|
// error handling
|
||
2 years ago
|
}
|
||
|
```
|
||
|
|
||
2 years ago
|
- Errors should be judged independently, not combined with other logic.
|
||
2 years ago
|
|
||
|
```go
|
||
|
// bad
|
||
|
v, err := foo()
|
||
2 years ago
|
if err != nil || v == nil {
|
||
|
// error handling
|
||
|
return err
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
//good
|
||
2 years ago
|
v, err := foo()
|
||
|
if err != nil {
|
||
2 years ago
|
// error handling
|
||
|
return err
|
||
2 years ago
|
}
|
||
|
|
||
|
if v == nil {
|
||
2 years ago
|
// error handling
|
||
|
return errors. New("invalid value v")
|
||
2 years ago
|
}
|
||
|
```
|
||
|
|
||
2 years ago
|
- If the return value needs to be initialized, use the following method.
|
||
2 years ago
|
|
||
|
```go
|
||
|
v, err := f()
|
||
|
if err != nil {
|
||
2 years ago
|
// error handling
|
||
|
return // or continue.
|
||
2 years ago
|
}
|
||
|
```
|
||
|
|
||
2 years ago
|
- Bug description suggestions
|
||
|
- Error descriptions start with a lowercase letter and do not end with punctuation, for example:
|
||
2 years ago
|
```go
|
||
2 years ago
|
// bad
|
||
|
errors.New("Redis connection failed")
|
||
|
errors.New("redis connection failed.")
|
||
2 years ago
|
|
||
2 years ago
|
// good
|
||
|
errors.New("redis connection failed")
|
||
2 years ago
|
```
|
||
2 years ago
|
- Tell users what they can do, not what they can't.
|
||
|
- When declaring a requirement, use must instead of should. For example, `must be greater than 0, must match regex '[a-z]+'`.
|
||
|
- When declaring that a format is incorrect, use must not. For example, `must not contain`.
|
||
|
- Use may not when declaring an action. For example, `may not be specified when otherField is empty, only name may be specified`.
|
||
|
- When quoting a literal string value, indicate the literal in single quotes. For example, `ust not contain '..'`.
|
||
|
- When referencing another field name, specify that name in backticks. For example, must be greater than `request`.
|
||
|
- When specifying unequal, use words instead of symbols. For example, `must be less than 256, must be greater than or equal to 0 (do not use larger than, bigger than, more than, higher than)`.
|
||
|
- When specifying ranges of numbers, use inclusive ranges whenever possible.
|
||
|
- Go 1.13 or above is recommended, and the error generation method is `fmt.Errorf("module xxx: %w", err)`.
|
||
2 years ago
|
|
||
2 years ago
|
### 1.4 panic processing
|
||
2 years ago
|
|
||
2 years ago
|
- Panic is prohibited in business logic processing.
|
||
|
- In the main package, panic is only used when the program is completely inoperable, for example, the file cannot be opened, the database cannot be connected, and the program cannot run normally.
|
||
|
- In the main package, use `log.Fatal` to record errors, so that the program can be terminated by the log, or the exception thrown by the panic can be recorded in the log file, which is convenient for troubleshooting.
|
||
|
- An exportable interface must not panic.
|
||
|
- It is recommended to use error instead of panic to convey errors in the package.
|
||
2 years ago
|
|
||
2 years ago
|
### 1.5 Unit Tests
|
||
2 years ago
|
|
||
2 years ago
|
- The unit test filename naming convention is `example_test.go`.
|
||
|
- Write a test case for every important exportable function.
|
||
|
- Because the functions in the unit test file are not external, the exportable structures, functions, etc. can be uncommented.
|
||
|
- If `func (b *Bar) Foo` exists, the single test function can be `func TestBar_Foo`.
|
||
2 years ago
|
|
||
2 years ago
|
### 1.6 Type assertion failure handling
|
||
2 years ago
|
|
||
2 years ago
|
- A single return value from a type assertion will panic for an incorrect type. Always use the "comma ok" idiom.
|
||
2 years ago
|
|
||
|
```go
|
||
|
// bad
|
||
|
t := n.(int)
|
||
|
|
||
2 years ago
|
//good
|
||
2 years ago
|
t, ok := n.(int)
|
||
|
if !ok {
|
||
2 years ago
|
// error handling
|
||
2 years ago
|
}
|
||
|
```
|
||
|
|
||
2 years ago
|
## 2. Naming convention
|
||
2 years ago
|
|
||
2 years ago
|
The naming convention is a very important part of the code specification. A uniform, short, and precise naming convention can greatly improve the readability of the code and avoid unnecessary bugs.
|
||
2 years ago
|
|
||
2 years ago
|
### 2.1 Package Naming
|
||
2 years ago
|
|
||
2 years ago
|
- The package name must be consistent with the directory name, try to use a meaningful and short package name, and do not conflict with the standard library.
|
||
|
- Package names are all lowercase, without uppercase or underscores, and use multi-level directories to divide the hierarchy.
|
||
|
- Item names can connect multiple words with dashes.
|
||
|
- Do not use plurals for the package name and the directory name where the package is located, for example, `net/url` instead of `net/urls`.
|
||
|
- Don't use broad, meaningless package names like common, util, shared or lib.
|
||
|
- The package name should be simple and clear, such as net, time, log.
|
||
2 years ago
|
|
||
2 years ago
|
### 2.2 Function Naming
|
||
2 years ago
|
|
||
2 years ago
|
- The function name is in camel case, and the first letter is uppercase or lowercase according to the access control decision,For example: `MixedCaps` or `mixedCaps`.
|
||
|
- Code automatically generated by code generation tools (such as `xxxx.pb.go`) and underscores used to group related test cases (such as `TestMyFunction_WhatIsBeingTested`) exclude this rule.
|
||
2 years ago
|
|
||
1 year ago
|
In accordance with the naming conventions adopted by OpenIM and drawing reference from the Google Naming Conventions as per the guidelines available at https://google.github.io/styleguide/go/, the following expectations for naming practices within the project are set forth:
|
||
|
|
||
|
1. **File Names:**
|
||
|
+ Both hyphens (`-`) and underscores (`_`) are permitted when naming files.
|
||
|
+ A preference is established for the use of underscores (`_`), suggesting it as the best practice in general scenarios.
|
||
|
2. **Script and Markdown Files:**
|
||
|
+ For shell scripts (bash files) and Markdown (`.md`) documents, the use of hyphens (`-`) is recommended.
|
||
|
+ This recommendation is based on the improved searchability and compatibility in web browsers when hyphens are used in names.
|
||
|
3. **Directories:**
|
||
|
+ A consistent approach is mandated for naming directories, exclusively using hyphens (`-`) to separate words within directory names.
|
||
|
|
||
|
|
||
2 years ago
|
### 2.3 File Naming
|
||
2 years ago
|
|
||
2 years ago
|
- Keep the filename short and meaningful.
|
||
|
- Filenames should be lowercase and use underscores to separate words.
|
||
2 years ago
|
|
||
2 years ago
|
### 2.4 Structure Naming
|
||
2 years ago
|
|
||
2 years ago
|
- The camel case is adopted, and the first letter is uppercase or lowercase according to the access control, such as `MixedCaps` or `mixedCaps`.
|
||
|
- Struct names should not be verbs, but should be nouns, such as `Node`, `NodeSpec`.
|
||
|
- Avoid using meaningless structure names such as Data and Info.
|
||
|
- The declaration and initialization of the structure should take multiple lines, for example:
|
||
2 years ago
|
|
||
|
```go
|
||
2 years ago
|
// User multi-line declaration
|
||
2 years ago
|
type User struct {
|
||
2 years ago
|
name string
|
||
|
Email string
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
// multi-line initialization
|
||
2 years ago
|
u := User{
|
||
2 years ago
|
UserName: "belm",
|
||
|
Email: "nosbelm@qq.com",
|
||
2 years ago
|
}
|
||
|
```
|
||
|
|
||
2 years ago
|
### 2.5 Interface Naming
|
||
2 years ago
|
|
||
2 years ago
|
- The interface naming rules are basically consistent with the structure naming rules:
|
||
|
- Interface names of individual functions suffixed with "er"" (e.g. Reader, Writer) can sometimes lead to broken English, but that's okay.
|
||
|
- The interface name of the two functions is named after the two function names, eg ReadWriter.
|
||
|
- An interface name for more than three functions, similar to a structure name.
|
||
2 years ago
|
|
||
2 years ago
|
For example:
|
||
2 years ago
|
|
||
|
```
|
||
2 years ago
|
// Seeking to an offset before the start of the file is an error.
|
||
|
// Seeking to any positive offset is legal, but the behavior of subsequent
|
||
|
// I/O operations on the underlying object are implementation-dependent.
|
||
|
type Seeker interface {
|
||
|
Seek(offset int64, whence int) (int64, error)
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
// ReadWriter is the interface that groups the basic Read and Write methods.
|
||
|
type ReadWriter interface {
|
||
|
reader
|
||
|
Writer
|
||
|
}
|
||
2 years ago
|
```
|
||
|
|
||
2 years ago
|
### 2.6 Variable Naming
|
||
2 years ago
|
|
||
2 years ago
|
- Variable names must follow camel case, and the initial letter is uppercase or lowercase according to the access control decision.
|
||
|
- In relatively simple (few objects, highly targeted) environments, some names can be abbreviated from full words to single letters, for example:
|
||
|
- user can be abbreviated as u;
|
||
|
- userID can be abbreviated as uid.
|
||
|
- When using proper nouns, the following rules need to be followed:
|
||
|
- If the variable is private and the proper noun is the first word, use lowercase, such as apiClient.
|
||
|
- In other cases, the original wording of the noun should be used, such as APIClient, repoID, UserID.
|
||
2 years ago
|
|
||
2 years ago
|
Some common nouns are listed below.
|
||
2 years ago
|
|
||
|
```
|
||
|
// A GonicMapper that contains a list of common initialisms taken from golang/lint
|
||
|
var LintGonicMapper = GonicMapper{
|
||
2 years ago
|
"API": true,
|
||
|
"ASCII": true,
|
||
|
"CPU": true,
|
||
|
"CSS": true,
|
||
|
"DNS": true,
|
||
|
"EOF": true,
|
||
|
"GUID": true,
|
||
|
"HTML": true,
|
||
|
"HTTP": true,
|
||
|
"HTTPS": true,
|
||
|
"ID": true,
|
||
|
"IP": true,
|
||
|
"JSON": true,
|
||
|
"LHS": true,
|
||
|
"QPS": true,
|
||
|
"RAM": true,
|
||
|
"RHS": true,
|
||
|
"RPC": true,
|
||
|
"SLA": true,
|
||
|
"SMTP": true,
|
||
|
"SSH": true,
|
||
|
"TLS": true,
|
||
|
"TTL": true,
|
||
|
"UI": true,
|
||
|
"UID": true,
|
||
|
"UUID": true,
|
||
|
"URI": true,
|
||
|
"URL": true,
|
||
|
"UTF8": true,
|
||
|
"VM": true,
|
||
|
"XML": true,
|
||
|
"XSRF": true,
|
||
|
"XSS": true,
|
||
|
}
|
||
|
```
|
||
|
|
||
|
- If the variable type is bool, the name should start with Has, Is, Can or Allow, for example:
|
||
|
|
||
|
```go
|
||
|
var has Conflict bool
|
||
2 years ago
|
var isExist bool
|
||
2 years ago
|
var can Manage bool
|
||
2 years ago
|
var allowGitHook bool
|
||
|
```
|
||
|
|
||
2 years ago
|
- Local variables should be as short as possible, for example, use buf to refer to buffer, and use i to refer to index.
|
||
|
- The code automatically generated by the code generation tool can exclude this rule (such as the Id in `xxx.pb.go`)
|
||
2 years ago
|
|
||
2 years ago
|
### 2.7 Constant naming
|
||
2 years ago
|
|
||
2 years ago
|
- The constant name must follow the camel case, and the initial letter is uppercase or lowercase according to the access control decision.
|
||
|
- If it is a constant of enumeration type, you need to create the corresponding type first:
|
||
2 years ago
|
|
||
|
```go
|
||
|
// Code defines an error code type.
|
||
|
type Code int
|
||
|
|
||
|
// Internal errors.
|
||
|
const (
|
||
2 years ago
|
// ErrUnknown - 0: An unknown error occurred.
|
||
|
ErrUnknown Code = iota
|
||
|
// ErrFatal - 1: An fatal error occurred.
|
||
1 year ago
|
ErrFatal
|
||
2 years ago
|
)
|
||
|
```
|
||
|
|
||
2 years ago
|
### 2.8 Error naming
|
||
2 years ago
|
|
||
2 years ago
|
- The Error type should be written in the form of FooError.
|
||
2 years ago
|
|
||
|
```go
|
||
|
type ExitError struct {
|
||
2 years ago
|
// ....
|
||
2 years ago
|
}
|
||
|
```
|
||
|
|
||
2 years ago
|
- The Error variable is written in the form of ErrFoo.
|
||
2 years ago
|
|
||
|
```go
|
||
2 years ago
|
var ErrFormat = errors. New("unknown format")
|
||
2 years ago
|
```
|
||
|
|
||
2 years ago
|
## 3. Comment specification
|
||
2 years ago
|
|
||
2 years ago
|
- Each exportable name must have a comment, which briefly introduces the exported variables, functions, structures, interfaces, etc.
|
||
|
- All single-line comments are used, and multi-line comments are prohibited.
|
||
|
- Same as the code specification, single-line comments should not be too long, and no more than 120 characters are allowed. If it exceeds, please use a new line to display, and try to keep the format elegant.
|
||
|
- A comment must be a complete sentence, starting with the content to be commented and ending with a period, `the format is // name description.`. For example:
|
||
2 years ago
|
|
||
|
```go
|
||
|
// bad
|
||
|
// logs the flags in the flagset.
|
||
2 years ago
|
func PrintFlags(flags *pflag. FlagSet) {
|
||
|
// normal code
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
//good
|
||
2 years ago
|
// PrintFlags logs the flags in the flagset.
|
||
2 years ago
|
func PrintFlags(flags *pflag. FlagSet) {
|
||
|
// normal code
|
||
2 years ago
|
}
|
||
|
```
|
||
|
|
||
2 years ago
|
- All commented out code should be deleted before submitting code review, otherwise, it should explain why it is not deleted, and give follow-up processing suggestions.
|
||
2 years ago
|
|
||
2 years ago
|
- Multiple comments can be separated by blank lines, as follows:
|
||
2 years ago
|
|
||
|
```go
|
||
|
// Package superman implements methods for saving the world.
|
||
|
//
|
||
|
// Experience has shown that a small number of procedures can prove
|
||
|
// helpful when attempting to save the world.
|
||
|
package superman
|
||
|
```
|
||
|
|
||
2 years ago
|
### 3.1 Package Notes
|
||
2 years ago
|
|
||
2 years ago
|
- Each package has one and only one package-level annotation.
|
||
1 year ago
|
- Package comments are uniformly commented with // in the format of `// Package <package name> package description`, for example:
|
||
2 years ago
|
|
||
|
```go
|
||
|
// Package genericclioptions contains flags which can be added to you command, bound, completed, and produce
|
||
|
// useful helper functions.
|
||
|
package genericclioptions
|
||
|
```
|
||
|
|
||
2 years ago
|
### 3.2 Variable/Constant Comments
|
||
2 years ago
|
|
||
2 years ago
|
- Each variable/constant that can be exported must have a comment description, `the format is // variable name variable description`, for example:
|
||
2 years ago
|
|
||
|
```go
|
||
|
// ErrSigningMethod defines invalid signing method error.
|
||
2 years ago
|
var ErrSigningMethod = errors. New("Invalid signing method")
|
||
2 years ago
|
```
|
||
2 years ago
|
- When there is a large block of constant or variable definition, you can comment a general description in front, and then comment the definition of the constant in detail before or at the end of each line of constant, for example:
|
||
2 years ago
|
```go
|
||
|
// Code must start with 1xxxxx.
|
||
|
const (
|
||
2 years ago
|
// ErrSuccess - 200: OK.
|
||
|
ErrSuccess int = iota + 100001
|
||
2 years ago
|
|
||
2 years ago
|
// ErrUnknown - 500: Internal server error.
|
||
|
ErrUnknown
|
||
2 years ago
|
|
||
2 years ago
|
// ErrBind - 400: Error occurred while binding the request body to the struct.
|
||
|
ErrBind
|
||
2 years ago
|
|
||
2 years ago
|
// ErrValidation - 400: Validation failed.
|
||
|
ErrValidation
|
||
2 years ago
|
)
|
||
|
```
|
||
2 years ago
|
### 3.3 Structure Annotation
|
||
2 years ago
|
|
||
2 years ago
|
- Each structure or interface that needs to be exported must have a comment description, the format is `// structure name structure description.`.
|
||
|
- The name of the exportable member variable in the structure, if the meaning is not clear, a comment must be given and placed before the member variable or at the end of the same line. For example:
|
||
2 years ago
|
|
||
|
```go
|
||
|
// User represents a user restful resource. It is also used as gorm model.
|
||
|
type User struct {
|
||
2 years ago
|
// Standard object's metadata.
|
||
|
metav1.ObjectMeta `json:"metadata,omitempty"`
|
||
2 years ago
|
|
||
2 years ago
|
Nickname string `json:"nickname" gorm:"column:nickname"`
|
||
|
Password string `json:"password" gorm:"column:password"`
|
||
|
Email string `json:"email" gorm:"column:email"`
|
||
|
Phone string `json:"phone" gorm:"column:phone"`
|
||
|
IsAdmin int `json:"isAdmin,omitempty" gorm:"column:isAdmin"`
|
||
2 years ago
|
}
|
||
|
```
|
||
|
|
||
2 years ago
|
### 3.4 Method Notes
|
||
2 years ago
|
|
||
2 years ago
|
Each function or method that needs to be exported must have a comment, the format is // function name function description., for examplelike:
|
||
2 years ago
|
|
||
|
```go
|
||
|
// BeforeUpdate run before update database record.
|
||
|
func (p *Policy) BeforeUpdate() (err error) {
|
||
2 years ago
|
// normal code
|
||
|
return nil
|
||
2 years ago
|
}
|
||
|
```
|
||
|
|
||
2 years ago
|
### 3.5 Type annotations
|
||
2 years ago
|
|
||
2 years ago
|
- Each type definition and type alias that needs to be exported must have a comment description, the format is `// type name type description.`, for example:
|
||
2 years ago
|
|
||
|
```go
|
||
|
// Code defines an error code type.
|
||
|
type Code int
|
||
|
```
|
||
|
|
||
2 years ago
|
## 4. Type
|
||
2 years ago
|
|
||
2 years ago
|
### 4.1 Strings
|
||
2 years ago
|
|
||
2 years ago
|
- Empty string judgment.
|
||
2 years ago
|
|
||
|
```go
|
||
|
// bad
|
||
|
if s == "" {
|
||
2 years ago
|
// normal code
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
//good
|
||
2 years ago
|
if len(s) == 0 {
|
||
2 years ago
|
// normal code
|
||
2 years ago
|
}
|
||
|
```
|
||
|
|
||
2 years ago
|
- `[]byte`/`string` equality comparison.
|
||
2 years ago
|
|
||
|
```go
|
||
|
// bad
|
||
|
var s1 []byte
|
||
|
var s2 []byte
|
||
|
...
|
||
|
bytes.Equal(s1, s2) == 0
|
||
|
bytes.Equal(s1, s2) != 0
|
||
|
|
||
2 years ago
|
//good
|
||
2 years ago
|
var s1 []byte
|
||
|
var s2 []byte
|
||
|
...
|
||
2 years ago
|
bytes. Compare(s1, s2) == 0
|
||
|
bytes. Compare(s1, s2) != 0
|
||
2 years ago
|
```
|
||
|
|
||
2 years ago
|
- Complex strings use raw strings to avoid character escaping.
|
||
2 years ago
|
|
||
|
```go
|
||
|
// bad
|
||
|
regexp.MustCompile("\\.")
|
||
|
|
||
2 years ago
|
//good
|
||
2 years ago
|
regexp.MustCompile(`\.`)
|
||
|
```
|
||
|
|
||
2 years ago
|
### 4.2 Slicing
|
||
2 years ago
|
|
||
2 years ago
|
- Empty slice judgment.
|
||
2 years ago
|
|
||
|
```go
|
||
|
// bad
|
||
|
if len(slice) = 0 {
|
||
2 years ago
|
// normal code
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
//good
|
||
2 years ago
|
if slice != nil && len(slice) == 0 {
|
||
2 years ago
|
// normal code
|
||
2 years ago
|
}
|
||
|
```
|
||
|
|
||
2 years ago
|
The above judgment also applies to map and channel.
|
||
2 years ago
|
|
||
2 years ago
|
- Declare a slice.
|
||
2 years ago
|
|
||
|
```go
|
||
|
// bad
|
||
|
s := []string{}
|
||
|
s := make([]string, 0)
|
||
|
|
||
2 years ago
|
//good
|
||
|
var s[]string
|
||
2 years ago
|
```
|
||
|
|
||
2 years ago
|
- slice copy.
|
||
2 years ago
|
|
||
|
```go
|
||
|
// bad
|
||
|
var b1, b2 []byte
|
||
|
for i, v := range b1 {
|
||
2 years ago
|
b2[i] = v
|
||
2 years ago
|
}
|
||
|
for i := range b1 {
|
||
2 years ago
|
b2[i] = b1[i]
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
//good
|
||
2 years ago
|
copy(b2, b1)
|
||
|
```
|
||
|
|
||
2 years ago
|
- slice added.
|
||
2 years ago
|
|
||
|
```go
|
||
|
// bad
|
||
|
var a, b []int
|
||
|
for _, v := range a {
|
||
2 years ago
|
b = append(b, v)
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
//good
|
||
2 years ago
|
var a, b []int
|
||
|
b = append(b, a...)
|
||
|
```
|
||
|
|
||
2 years ago
|
### 4.3 Structure
|
||
2 years ago
|
|
||
2 years ago
|
- struct initialization.
|
||
2 years ago
|
|
||
2 years ago
|
The struct is initialized in multi-line format.
|
||
2 years ago
|
|
||
|
```go
|
||
|
type user struct {
|
||
2 years ago
|
Id int64
|
||
|
name string
|
||
2 years ago
|
}
|
||
|
|
||
|
u1 := user{100, "Colin"}
|
||
|
|
||
|
u2 := user{
|
||
2 years ago
|
Id: 200,
|
||
|
Name: "Lex",
|
||
2 years ago
|
}
|
||
|
```
|
||
|
|
||
2 years ago
|
## 5. Control Structure
|
||
2 years ago
|
|
||
|
### 5.1 if
|
||
|
|
||
2 years ago
|
- if accepts the initialization statement, the convention is to create local variables in the following way.
|
||
2 years ago
|
|
||
|
```go
|
||
|
if err := loadConfig(); err != nil {
|
||
2 years ago
|
// error handling
|
||
|
return err
|
||
2 years ago
|
}
|
||
|
```
|
||
|
|
||
2 years ago
|
- if For variables of bool type, true and false judgments should be made directly.
|
||
2 years ago
|
|
||
|
```go
|
||
|
var isAllow bool
|
||
|
if isAllow {
|
||
2 years ago
|
// normal code
|
||
2 years ago
|
}
|
||
|
```
|
||
|
|
||
|
### 5.2 for
|
||
|
|
||
2 years ago
|
- Create local variables using short declarations.
|
||
2 years ago
|
|
||
|
```go
|
||
|
sum := 0
|
||
|
for i := 0; i < 10; i++ {
|
||
2 years ago
|
sum += 1
|
||
2 years ago
|
}
|
||
|
```
|
||
|
|
||
2 years ago
|
- Don't use defer in for loop, defer will only be executed when the function exits.
|
||
2 years ago
|
|
||
|
```go
|
||
|
// bad
|
||
|
for file := range files {
|
||
2 years ago
|
fd, err := os. Open(file)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
defer fd. Close()
|
||
|
// normal code
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
//good
|
||
2 years ago
|
for file := range files {
|
||
2 years ago
|
func() {
|
||
|
fd, err := os. Open(file)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
defer fd. Close()
|
||
|
// normal code
|
||
|
}()
|
||
2 years ago
|
}
|
||
|
```
|
||
|
|
||
|
### 5.3 range
|
||
|
|
||
2 years ago
|
- If only the first item (key) is needed, discard the second.
|
||
2 years ago
|
|
||
|
```go
|
||
1 year ago
|
for keyIndex := range keys {
|
||
2 years ago
|
// normal code
|
||
|
}
|
||
|
```
|
||
|
|
||
2 years ago
|
- If only the second item is required, underline the first item.
|
||
2 years ago
|
|
||
|
```go
|
||
|
sum := 0
|
||
|
for _, value := range array {
|
||
2 years ago
|
sum += value
|
||
2 years ago
|
}
|
||
|
```
|
||
|
|
||
|
### 5.4 switch
|
||
|
|
||
2 years ago
|
- must have default.
|
||
2 years ago
|
|
||
|
```go
|
||
|
switch os := runtime.GOOS; os {
|
||
2 years ago
|
case "linux":
|
||
|
fmt.Println("Linux.")
|
||
|
case "darwin":
|
||
|
fmt.Println("OS X.")
|
||
|
default:
|
||
|
fmt.Printf("%s.\n", os)
|
||
2 years ago
|
}
|
||
|
```
|
||
|
|
||
|
### 5.5 goto
|
||
2 years ago
|
- Business code prohibits the use of goto.
|
||
|
- Try not to use frameworks or other low-level source code.
|
||
2 years ago
|
|
||
2 years ago
|
## 6. Functions
|
||
2 years ago
|
|
||
2 years ago
|
- Incoming variables and return variables start with a lowercase letter.
|
||
|
- The number of function parameters cannot exceed 5.
|
||
|
- Function grouping and ordering
|
||
|
- Functions should be sorted in rough calling order.
|
||
|
- Functions in the same file should be grouped by receiver.
|
||
|
- Try to use value transfer instead of pointer transfer.
|
||
|
- The incoming parameters are map, slice, chan, interface, do not pass pointers.
|
||
2 years ago
|
|
||
2 years ago
|
### 6.1 Function parameters
|
||
2 years ago
|
|
||
2 years ago
|
- If the function returns two or three arguments of the same type, or if the meaning of the result is not clear from the context, use named returns, otherwise it is not recommended to use named returns, for example:
|
||
2 years ago
|
|
||
|
```go
|
||
|
func coordinate() (x, y float64, err error) {
|
||
2 years ago
|
// normal code
|
||
2 years ago
|
}
|
||
|
```
|
||
2 years ago
|
- Both incoming and returned variables start with a lowercase letter.
|
||
|
- Try to pass by value instead of pointer.
|
||
|
- The number of parameters cannot exceed 5.
|
||
|
- Multiple return values can return up to three, and if there are more than three, please use struct.
|
||
2 years ago
|
|
||
|
### 6.2 defer
|
||
|
|
||
2 years ago
|
- When resources are created, resources should be released immediately after defer (defer can be used boldly, the performance of defer is greatly improved in Go1.14 version, and the performance loss of defer can be ignored even in performance-sensitive businesses).
|
||
|
- First judge whether there is an error, and then defer to release resources, for example:
|
||
2 years ago
|
|
||
|
```go
|
||
2 years ago
|
rep, err := http. Get(url)
|
||
2 years ago
|
if err != nil {
|
||
2 years ago
|
return err
|
||
2 years ago
|
}
|
||
|
|
||
|
defer resp.Body.Close()
|
||
|
```
|
||
|
|
||
2 years ago
|
### 6.3 Method Receiver
|
||
2 years ago
|
|
||
2 years ago
|
- It is recommended to use the lowercase of the first English letter of the class name as the name of the receiver.
|
||
|
- Don't use a single character in the name of the receiver when the function exceeds 20 lines.
|
||
|
- The name of the receiver cannot use confusing names such as me, this, and self.
|
||
2 years ago
|
|
||
2 years ago
|
### 6.4 Nesting
|
||
|
- The nesting depth cannot exceed 4 levels.
|
||
2 years ago
|
|
||
2 years ago
|
### 6.5 Variable Naming
|
||
|
- The variable declaration should be placed before the first use of the variable as far as possible, following the principle of proximity.
|
||
|
- If the magic number appears more than twice, it is forbidden to use it and use a constant instead, for example:
|
||
2 years ago
|
|
||
|
```go
|
||
2 years ago
|
// PI...
|
||
|
const Price = 3.14
|
||
2 years ago
|
|
||
|
func getAppleCost(n float64) float64 {
|
||
2 years ago
|
return Price * n
|
||
2 years ago
|
}
|
||
|
|
||
|
func getOrangeCost(n float64) float64 {
|
||
2 years ago
|
return Price * n
|
||
2 years ago
|
}
|
||
|
```
|
||
|
|
||
2 years ago
|
## 7. GOPATH setting specification
|
||
|
- After Go 1.11, the GOPATH rule has been weakened. Existing code (many libraries must have been created before 1.11) must conform to this rule. It is recommended to keep the GOPATH rule to facilitate code maintenance.
|
||
|
- Only one GOPATH is recommended, multiple GOPATHs are not recommended. If multiple GOPATHs are used, the bin directory where compilation takes effect is under the first GOPATH.
|
||
2 years ago
|
|
||
2 years ago
|
## 8. Dependency Management
|
||
2 years ago
|
|
||
2 years ago
|
- Go 1.11 and above must use Go Modules.
|
||
|
- When using Go Modules as a dependency management project, it is not recommended to submit the vendor directory.
|
||
|
- When using Go Modules as a dependency management project, the go.sum file must be submitted.
|
||
2 years ago
|
|
||
2 years ago
|
### 9. Best Practices
|
||
2 years ago
|
|
||
2 years ago
|
- Minimize the use of global variables, but pass parameters, so that each function is "stateless". This reduces coupling and facilitates division of labor and unit testing.
|
||
|
- Verify interface compliance at compile time, for example:
|
||
2 years ago
|
|
||
|
```go
|
||
|
type LogHandler struct {
|
||
2 years ago
|
h http.Handler
|
||
|
log *zap. Logger
|
||
2 years ago
|
}
|
||
2 years ago
|
var_http.Handler = LogHandler{}
|
||
2 years ago
|
```
|
||
2 years ago
|
- When the server processes a request, it should create a context, save the relevant information of the request (such as requestID), and pass it in the function call chain.
|
||
2 years ago
|
|
||
2 years ago
|
### 9.1 Performance
|
||
|
- string represents an immutable string variable, modifying string is a relatively heavy operation, and basically needs to re-apply for memory. Therefore, if there is no special need, use []byte more when you need to modify.
|
||
|
- Prefer strconv over fmt.
|
||
2 years ago
|
|
||
2 years ago
|
### 9.2 Precautions
|
||
2 years ago
|
|
||
2 years ago
|
- append Be careful about automatically allocating memory, append may return a newly allocated address.
|
||
|
- If you want to directly modify the value of the map, the value can only be a pointer, otherwise the original value must be overwritten.
|
||
|
- map needs to be locked during concurrency.
|
||
|
- The conversion of interface{} cannot be checked during compilation, it can only be checked at runtime, be careful to cause panic.
|