This is a creation in Article, where the information may have evolved or changed.
Gofmt
Most of the formatting problems can be solved by GOFMT, GOFMT automatically format the code, to ensure that all the go code and the official recommended format is consistent, so all the format of the problem, the results of the gofmt will prevail.
President
A line with a maximum of 80 characters, more than the use of line display, try to keep the format elegant.
* Notes
In the coding phase, you should write the comments of variables, functions, packages, and then export the document using Godoc. The comment must be a complete sentence, and the end of the sentence should end with a period (full period). Comments recommended in English, you can write code in the process of exercise in English reading and writing skills. And there is no problem of coding in English.
Each package should have a package comment, a block comment or a line comment that precedes the packages clause. Package if you have more than one go file, you only need to appear in a go file.
ping包实现了常用的pingping
To export a function note, the first statement should be a generalized statement and begin with the declared name.
// 求a和b的和,返回sum。func Myfunction(sum int) (a, b int) {
Named
- Names that need to be supplemented by annotations are not good names.
- Use searchable names: single-letter names and numeric constants are hard to find from a whole bunch of text. A single-letter name applies only to local variables in a short method, and the name should correspond to its scope. If a variable or constant may be used in multiple places in your code, you should assign it to a name that is easy to search.
- Make a meaningful distinction: there is no difference between product and ProductInfo and Productdata, there is no difference between namestring and name, and to differentiate names, distinguish them by the way in which the reader can identify the differences.
- Function naming rules: Hump-named, the name can be long but the function, the necessary parameters are clearly described, the function name should be a verb or a verb phrase, such as postpayment, Deletepage, save. And according to JavaBean Standard plus get, set, is prefix. For example: XXX + with + required parameter name + and + required parameter name + ....
- struct naming rules: struct name should be noun or noun phrase, such as custome, Wikipage, account, Addressparser, avoid using manager, Processor, Data, Info, such class name, class name should not be a verb.
- Package name naming rules: Package names should be lowercase words, do not use underscores, or mix case.
- Interface naming rules: the interface name of a single function is named "ER" as a suffix, such as reader,writer. The implementation of the interface removes the "ER".
type Reader interface { Read(p []byte) (n int, err error)}
Two functions Interface name synthesis two function names
type WriteFlusher interface { Write([]byte) (int, error) Flush() error}
The interface name of more than three functions, abstract the function of this interface, similar to the structure name
type Car interface { Start([]byte) Stop() error Recover()}
Constants need to be made up of all uppercase letters and use underscore participles:
const"1.0"
If you are a constant of an enumeration type, you need to create the appropriate type first:
typestringconst ( HTTP "http" "https")
In cases where the functionality of the module is more complex and the constant name is easily confused, the full prefix can be used to better differentiate the enumeration type:
typeintconst ( iota PULL_REQUEST_STATUS_CHECKING PULL_REQUEST_STATUS_MERGEABLE)
Variable
Variable naming basically follows the corresponding English expression or shorthand, in a relatively simple environment (small number of objects, targeted), you can use some names from the full word to a single letter, for example:
- User can be abbreviated as U
- UserID can be abbreviated UID
If the variable type is type bool, the name should start with the has, is, Can, or allow:
varboolvarboolvarboolvarbool
Variable Naming conventions
Variable names generally follow the Hump method, but when you encounter a particular noun, you need to follow these rules:
- If the variable is private and the name is the first word, use lowercase, such as apiclient
- Other circumstances should use the original wording of the noun, such as apiclient, Repoid, UserID
- Error Example: Urlarray, should be written as Urlarray or Urlarray
Some common nouns are listed below:
A Gonicmapper thatcontains aList ofCommon initialisms taken fromGolang/lintvar Lintgonicmapper = gonicmapper{"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,}
The struct declaration and initialization format takes multiple lines:
Defined as follows:
type User struct{ Username string Email string}
Initialize as follows:
u := User{ "test", Email: "test@gmail.com",}
iferr := file.Chmod(0664err != nil { err}
For
Creating local variables with short declarations
0fori0i10i{ sum += i}
Return
Return as soon as possible: Once an error occurs, go back immediately
err := os.Open(name)iferr != nil { errerr := f.Stat()iferr != nil { f.Close() err}codeUsing(f, d)
ifnil { // error handling else { // normal code }
Use the following notation
ifnil { // error handling return// or continue, etc. } // normal code
When using the return value of a function, use the following method
x, err := f()ifnil { // error handling return}// use x
Try not to use panic unless you know what you are doing
The import package is managed in groups, separated by newline characters, and the standard library as the first group of groups. If your package introduces three types of packages, standard library packages, program internals, and third party packages, it is recommended that you organize your packages in the following ways
package mainimport ( "fmt" "os" "kmg/a" "kmg/b" "code.google.com/a" "github.com/b")
Do not use relative paths to introduce packages in your project:
// 错误示例import “../net”// 正确的做法import “github.com/repo/proj/src/net”
Goimports will automatically format it for you.
Parameter passing
- For small amounts of data, do not pass pointers
- For structs with large amounts of data, you can consider using pointers
- The incoming parameter is Map,slice,chan do not pass the pointer because Map,slice,chan is a reference type and does not require a pointer to pass the pointer
Unit Test
The function name for the
Unit test file name specification for EXAMPLE_TEST.GO
Test case must start with test, for example: Testexample