- variable
- The more important variable, the longer the life cycle, the longer variable names
- Variable names should not contain the type of information
- Variable names and default values should be consistent
- For example
isCacheDsiabled
thanisCacheEnabled
good, because the default value isfalse
- For example
- By default the first letter should be lowercase, to minimize the scope
- Packet may be used to declare variables inside a function
- Non-export global variables should begin with an underscore
- Avoid the same name and function of the internal variables lead to unpredictable bug
- Embedded type (e.g. mutex) should be placed at the beginning of the structure, and conventional and separate fields
- String for formatting be placed outside printf, and modified by const
- Try to use
type
to create a new specific type instead of the broad[]int
basic types- Benefits can customize the new method
- function
- Function should not exceed 50 lines
- A function should do only one thing
- Use interface parameters let function define their own behavior it needs
- If you open a coroutine function should return a caller to a close coroutine function
- Try not to use named return value
- If you want to ignore the return value, it should be used
_ = f()
to display points out - The for-select statement into a function
- method
- If you do not know the value of the use of the recipient or the recipient pointer, pointers recipients
- Complicated by
- Thread-safe way to create something that is the best choice
sync.Once
- For example configuration initialization
- Thread-safe way to create something that is the best choice
- The internet
- Always turn off the body http:
defer r.Body.Close()
- Should first check the HTTP response error is nil, then call resp.Body.Close () to close the response body
- Always turn off the body http:
- init function
- Do not perform operations such as initialization in the init function
- If another package depends on the current package, then the introduction of the dependency of engineers can be very confusing when it encounters an error
- I should not have done heavy initialization logic in the init, but to do some simple pre-conditional
- Do not perform operations such as initialization in the init function
- Error Handling
- You can carry up some additional information errors.Wrap throwing error convenient top judge
- other
- Consider instead of multiple if statements with switch
- Struct {} used to transmit Chan signal, chan bool expression not clear
- Each IO function blocked or operation should be canceled or at least timeout
- If you want to compare a timestamp, use
time.Before
ortime.After
do not usetime.Sub
to get the DURATION , then check its value - Do not forget to stop ticker
ticker := time.NewTicker(1 * time.Second); defer ticker.Stop()
- Analyzing slice is empty, we are determined length is not nil
- When a new slice cut from a slice, a new slice to re-declare and copy the old slice
copy(new, old)
- Otherwise, the old BOTTOM array does not release, resulting in excessive memory
reference
Go language practice: It is recommended to write maintainable programs