Golang Coding Specification

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Note: This article is a coding specification agreed by the author's team, and is organized in accordance with official guidelines effective Golang and Golang code Review comments to align with official and community coding styles.

Gofmt

Most of the formatting problems can be solved by GOFMT, GOFMT automatically format the code, guaranteeing all the go code consistent format.

Normally, when writing go code with sublime, the plug-in Gosublilme has called GOFMT to format the code.

Comments

The variables, functions, and package annotations are written synchronously during the encoding phase, and annotations can be generated from the Godoc export.

The comment must be a complete sentence, starting with the content that needs the comment, and ending with a period.

Each exported (uppercase) name in the program should have a document comment.

    • Package annotations

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.

    • Types that can be exported

The first statement should be a summary statement and use the declared name as the starting line.

// Compile parses a regular expression and returns, if successful, a Regexp// object that can be used to match against text.func Compile(str string) (regexp *Regexp, err error) {

Named

With short naming, long names do not automatically make things easier to read, and document annotations are more useful than extra-long names.

    • Package Name

The package name should be a lowercase word, not underlined or mixed case.

    • Interface Name

The interface name of a single function is suffixed with "er", 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}

Interface names of more than three functions, similar to struct names

type Car interface {    Start([]byte)     Stop() error    Recover()}
    • Mixed case

Use hump-style naming

MixedCaps 大写开头,可导出mixedCaps 小写开头,不可导出
    • Variable

      全局变量:驼峰式,结合是否可导出确定首字母大小写参数传递:驼峰式,小写字母开头局部变量:下划线形式

Control structure

    • If

If the initialization statement is accepted, the Convention establishes the local variable as follows

if err := file.Chmod(0664); err != nil {    return err}
    • For

Creating local variables with short declarations

sum := 0for i := 0; i < 10; i++ {    sum += i}
    • Range

If you only need the first item (key), discard the second one:

for key := range m {    if key.expired() {        delete(m, key)    }}

If only the second item is required, place the first item as an underscore

sum := 0for _, value := range array {    sum += value}
    • Return

Return as soon as possible: Once an error occurs, go back immediately

f, err := os.Open(name)if err != nil {    return err}d, err := f.Stat()if err != nil {    f.Close()    return err}codeUsing(f, d)

function (required)

    • The function takes a named multi-valued return
    • Incoming variables and return variables start with lowercase letters

      func nextInt(b []byte, pos int) (value, nextPos int) {

      In Godoc-generated documents, function declarations with return values are more conducive to understanding

Error handling

    • Error is returned as the value of the function, and error must be handled
    • Error description If English must be lowercase, punctuation ends are not required
    • Processing with a separate error stream

Do not use this method

    if err != nil {        // error handling    } else {        // normal code    }

Instead, use the following method

    if err != nil {        // error handling        return // or continue, etc.    }    // normal code

If the return value needs to be initialized, use the following method

x, err := f()if err != nil {    // error handling    return}// use x

Panic

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

Import

    • Group management of import packages, and the standard library as the first group

      package mainimport (    "fmt"    "hash/adler32"    "os"    "appengine/user"    "appengine/foo"    "code.google.com/p/x/y"    "github.com/foo/bar")

Goimports enables automatic formatting of the

Abbreviation

    • Use all uppercase or lowercase to denote abbreviated words

For example, for the word URL, do not use

UrlPony

and to use

urlPony 或者 URLPony  

Parameter passing

    • For small amounts of data, do not pass pointers
    • For structs with large amounts of data, you can consider using pointers
    • Incoming parameter is Map,slice,chan do not pass pointer

Because Map,slice,chan is a reference type, you do not need a pointer to pass the pointer

Accepted by

    • Name

Unify single letter ' P ' instead of this,me or self

    type T struct{}     func (p *T)Get(){}
    • Type

For Go beginners, the type of recipient if not clear, unified use of pointer-type

func (p *T)Get(){}

Instead of

func (p T)Get(){}   

In some cases, for performance reasons, or for types that are reference types, there are some exceptions

    • If the recipient is Map,slice or Chan, do not pass the pointer
//Mappackage mainimport (    "fmt")type mp map[string]stringfunc (m mp) Set(k, v string) {    m[k] = v}func main() {    m := make(mp)    m.Set("k", "v")    fmt.Println(m)}
Channelpackage mainimport ("FMT") type Ch chan interface{}func (c ch) Push (i interface{}) {C <-i}func (c ch) Pop () interface{} {return <-c}func main () {c: = Make (ch, 1) c.push ("I") fmt. Println (C.pop ())}<pre><code><br/></code></pre> If you need to modify the slice, re-assign the value by returning the value/ Slicepackage mainimport ("FMT") type slice []bytefunc main () {s: = make (slice, 0) s = S.addone () fmt. Println (s)}func (s slice) AddOne (b byte) []byte {return append (s, b)}<pre><code><br/></code>& Lt;/pre> If the recipient contains sync. A mutex or struct similar to a synchronization field must use pointer passing to avoid copying package mainimport ("Sync") type T struct {m sync. Mutex}func (t *t) lock () {t.m.lock ()}/*wrong!!! Func (T-T) lock () {T.m.lock ()}*/func main () {t: = new (t) T.lock ()}<pre><code><br/></code& Gt;</pre> if the recipient is a large structure or an array, it is more efficient to pass the pointer. Package Mainimport ("FMT") type T struct {data [1024]byte}func (t *t) Get () byte {return t.data[0]}funC Main () {t: = new (t) fmt. Println (T.get ())}
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.