当前位置:网站首页>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
原网站

版权声明
本文为[AcTarjan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202211319438144.html