Coding Rules

[!TIP] This document is machine-translated by Google. If you find grammatical and semantic errors, and the document description is not clear, please PR

import

  • Single-line import is not recommended being wrapped in parentheses
  • Introduce in the order of Official Package, NEW LINE, Project Package, NEW LINE, Third Party Dependent Package

    1. import (
    2. "context"
    3. "string"
    4. "greet/user/internal/config"
    5. "google.golang.org/grpc"
    6. )

Function returns

  • Object avoids non-pointer return
  • Follow the principle that if there is a normal value return, there must be no error, and if there is an error, there must be no normal value return.

Error handling

  • An error must be handled, if it cannot be handled, it must be thrown.
  • Avoid underscore (_) receiving error

Function body coding

  • It is recommended that a block end with a blank line, such as if, for, etc.

    1. func main (){
    2. if x==1{
    3. // do something
    4. }
    5. fmt.println("xxx")
    6. }
  • Blank line before return

    1. func getUser(id string)(string,error){
    2. ....
    3. return "xx",nil
    4. }