1. Gofmt command

Most formatting problems can be solved by gofMT. Gofmt automatically formats the code to ensure that all go code is consistent with the official recommended format. All formatting problems are subject to goFMT results. Therefore, it is recommended to run this command before committing the code base.

2. The governor

The maximum length of a line should not exceed 80 characters. If there are more than 80 characters, use line breaks to keep the format elegant.

3. Comments

During the coding phase, variables, functions and package comments should be written synchronously. Finally, godoc command can be used to export documents. Comments must be complete sentences, and sentences should end with a full stop. Comments are recommended in English, so that you can practice your English reading and writing skills in the process of writing code. And there are no coding problems in English.

Each package should have a package comment, a block comment or line comment that precedes the package clause. If a package has multiple GO files, it only needs to appear in one GO file.

// Ping implements the common ping-related function package pingCopy the code

Named after 4.

Names that need to be supplemented by comments are not good names. Use searchable names: Single-letter names and numeric constants are difficult to search through a large volume of text. Single-letter names apply only to local variables in short methods, and the name length should correspond to its scope. If a variable or constant may be used in more than one place in the code, give it a name that makes it easier to search.

Make a meaningful distinction: there is no difference between Product and ProductInfo and ProductData, and there is no difference between NameString and Name. To distinguish names, it is necessary to distinguish them in a way that readers can identify the differences.

Function naming rules: the name can be long but must clearly describe the function and necessary parameters. The function name should be a verb or phrasal verb, such as postPayment, deletePage, save. And according to the Javabean standard with get, set, is prefix. For example, XXX + With + Required parameter name + And + Required parameter name +... . Structure naming rules: Structure names should be nouns or noun phrases such as Custome, WikiPage, Account, AddressParser. Avoid Manager, Processor, Data, Info, etc. Class names should not be verbs. Package name naming rules: Package names should be lowercase words, do not use underscores or mixed case. Interface naming rules: The interface names of a single function, such as Reader or Writer, are suffixed with er. The interface is implemented without "er". Copy the codeCopy the code
	typeReader interface {Read(p []byte) (n int, err Error)type WriteFlusher interface {
    	Write([]byte) (int, error)
    	Flush() error
	}
Copy the code

5. Constant

Constants must be in all uppercase letters and underlined:

	const APP_VER = "1.0"// If it is a constant of enumerated type, you need to create the corresponding type:type Scheme string

	const (
   	 HTTP  Scheme = "http"
    	HTTPS Scheme = "https"
	)
Copy the code

6. The variable

Variable naming basically follows the corresponding English expressions or abbreviations. In relatively simple environments (with a small number of objects and strong targets), some names can be shortened from full words to single letters, for example:

// If the variable type Is bool, the name should start with Has, Is, Can, or Allow:  var isExist bool var hasConflict bool var canManage bool var allowGitHook boolCopy the code

7. Variable naming conventions

Variable names generally follow the hump method, but when it comes to unique nouns, follow these rules:

If the variable is private and the first word of the noun is unique, use lowercase,

For example, in all other cases the noun should be written in its original form.

For example, APIClient, repoID, UserID Error example: UrlArray, should be written as UrlArray or UrlArray

// Here are some common nouns:"API"."ASCII"."CPU"."CSS"."DNS"."EOF"The GUID","HTML","HTTP","HTTPS","ID","IP","JSON","LHS","QPS","RAM","RHS""RPC","SLA","SMTP","SSH","TLS","TTL","UI","UID","UUID","URI","URL","UTF8","VM","XML","XSRF","XSS"
Copy the code

8. Struct

U := User{Username: "test", Email: "[email protected]", }Copy the code

9. panic

Try not to use Panic unless you know what you are doing

10. import

Import packages are grouped, separated by newlines, and the standard library is the first group of grouping. If your package introduces three types of packages: library packages, in-app packages, and third-party packages, it is recommended to organize your package in the following way

Package main import ("fmt"
    "os"

    "kmg/a"
    "kmg/b"

    "code.google.com/a"
    "github.com/b"Copying the code Goimports will automatically format it for youCopy the code

11. Parameter transfer

For small amounts of data, do not pass Pointers For structs with large amounts of data you can consider using Pointers. The parameters passed in are map, slice, and chan. Do not pass Pointers because map, slice, and chan are reference types and do not need to pass Pointers

Unit testing

Unit Test file name Naming conventions: example_test.go Test case function names must start with Test, for example, func TestExampleCopy the code