Go language naming convention

First, the variable naming convention

    Variable names commonly used in camel, when faced with a unique noun (acronym or abbreviation, such as DNS), the private-specific terms, depending on whether all uppercase or lowercase. example:

was apiClient 
was urlString

Second, the constant naming conventions

    With variable rules, semantic integrity and strive to clear, not too long name.
    If the module is a complex, to avoid confusion, the function may be defined in a unified file in the package.

todayNews = const "the Hello" 
 
// if more than one constant method should be used to organize parentheses 
 
const ( 
  systemName = "the What" 
  SYSVAL = "dasdsada" 
)

Third, the interface naming conventions

    Interface to a single function name suffix er

 

Reader interface {type 
    the Read (P [] byte) (n-int, ERR error) 
} 
interface name two integrated function name two functions, such as: 

type interface {WriteFlusher 
    the Write ([] byte) (int, error) 
    the Flush ( ) error 
} 
interface name structure name of three or more similar functions, such as: 

type interface Car { 
    the Start () 
    the Stop () 
    Drive () 
}

 

Fourth, the structure of the naming conventions

    Structure names should be nouns or noun phrases, such as Account, Book, avoid the use of such Manager. If the data structures need to be serialized, such as JSON, the initials, which includes fields.

Five, receiver naming convention

    The concept of existence receiver golang in  the name of Receiver should acronym, should be as consistent as possible, to avoid this, super, and some other languages as semantic keywords

 

type A struct{}
 
func (a *A) methodA() {
}
func (a *A) methodB() {
  a.methodA()
}

 

Six function / method of naming

 

Due to the particularity Golang (sensitive to control by the visibility function), in addition to the special function unit tests the performance test, the following principles should

 

  1. Using Camel. The functions and the necessary parameters reflected in the name, not too long, as updateById, getUserInfo.
  2. If the outer packages do not need to access functions Please begin with a lowercase
  3. If you need to expose to the outside of the package is required to access the function names begin with a capital of

 

A typical function is named as follows:

 

Always use a double slash // Note, the exposed method of the object 
FUNC (* fileDao) AddFile (File * model.File) {BOOL 
  Result: = db.NewRecord (File *) 
  IF {Result 
   db.Create (File) 
  } 
  return Result 
} 
  
// do not need access to the outside of the package as the function 
FUNC removeCommaAndQuote (Content String) String { 
  Re, _: = regexp.Compile ( "[\\ \\`,] + ") 
  return strings.TrimSpace (re.ReplaceAllString ( Content, "")) 
}

 

Seven Notes naming convention

 

Each package should have a package comment, before the package is located. If the same package multiple files, you only need to write in a file; if you want to head in each file with comments, you need to add a blank line in the comments and copyright front Package, or copyright notices will Package of as a comment. Such as:

// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package net

 

Each begins with a capital letter (ie, can be derived) method should be annotated, and begins with the function name. Such as:

// Get response will correspond to route forwarding the get request 
FUNC (the Controller C *) the Get () { 
    ... 
}

 

Start with a capital letter that the method is a public method available to call, if you want only way out of use in this package, developed in lowercase letters. Such as:

func (c *Controller) curl() {
    ...
}

 

Comments should use a complete sentence, the first word of comment should be to comment indicator, so easy to find in godoc in.

 

Notes should be a period End.

 

Eight, package naming conventions

    Package name in lower case, use short names, as far as possible, and the standard library do not conflict. Unified package name singular form.

Nine, file naming convention 

    Since the file without any relationship with the package, but avoid the problem of windows sensitive, so the recommended file specification as follows:

    The file name should always use lowercase, with an underscore split between different words, no camel, named should see the name EENOW as possible. Try to see the name implies, saw the file name you can know about the content of this document under which the test files that end _test.go, in addition to the test file, named _ does not appear.

 

Guess you like

Origin www.cnblogs.com/zhangyafei/p/12466162.html