Go Language Programming Specification

Source: Internet
Author: User
Tags naming convention

1. GOFMT command

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, all formats related issues, the results of gofmt will prevail. Therefore, it is recommended that you run this command before you commit the code base.

2. President

A line with a maximum of 80 characters, more than the use of line display, try to keep the format elegant.

3. Notes

In the coding phase, you should write the comments for variables, functions, packages, and then export the document using the Godoc command. 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 Packages implement common Ping-related functions
4. Naming
    • naming that needs to be annotated to supplement is not a good name.
    • uses searchable names: single-letter names and numeric constants are difficult to search through 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.
    • makes 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 verb or 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, The 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 convention: the interface name of a single function is suffixed with "er", such as Reader,writer. The implementation of the interface removes the "ER".
Interface {        Read (p []byteint, err Error)}  //  multiple function interfaces interface  {    Write ([]byte) (int, error)    Flush () error}
5. Constants

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:

string Const (   http  "http"    "https"
)  
6. Variables

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:
var BOOL var BOOL var BOOL var BOOL      
7. 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:

"API","ASCII","CPU","CSS","DNS","EOF"Guid","Html","HTTP","HTTPS","Id","IP","JSON","LHS","QPS","RAM","RHS "    "RPC"  ,"SLA","SMTP","SSH" ,"TLS","TTL"       ,"UI","UID","UUID","URI","URL","UTF8","VMS","XML" ,"XSRF ","XSS"      
8. struct specification

The struct declaration and initialization format takes multiple lines and is defined as follows:

struct {    Username  string e-    mail     string  }

Initialize as follows:

U: = user{   "test",    email:    "[email protected ]",}
9. Panic

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

. Import

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")

Goimports will automatically format it for you.

11. Parameter passing
    • For small amounts of data, do not pass pointers
    • For structs with large amounts of data, you can consider using pointers
    • The parameter passed in 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
12. Unit Testing

Unit test file name naming specification:

Example_test.go

The function name of the test case must begin with test, for example:

Func testexample

Go Language Programming Specification

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.