This is a creation in Article, where the information may have evolved or changed.
Before starting a new language, you need to understand the syntax of the language ( if you have programming knowledge in other languages ), and before you start learning, let's learn about the format of Go .
If you all have a unified coding style, you can make it easier to maintain someone else's code. At the same time we execute a FMT command before committing the code in order to submit a uniform style of code.
Comments
c language-style " //" block comment, also supports c++ -style line annotations while using /**/ Commenting on the package We look at string package source code, using go The source code under uses This time the mind is not encoded when the clarity of the recovery is always easy to lose something
1//Copyright: the Go Authors. All rights reserved. 2// Use of the This source code was governed by a Bsd-style 3//license that can is found in the license file. 4 5//package strings implements simple functions to manipulate strings. 6 Package strings 7 8 import ( 9 "Unicode" ten "UTF8" One ) A //explode splits s into an array of UTF-8 sequences, one per Unicode character (still strings) up to a Maximum of N (n < 0 means no limit). //Invalid UTF-8 sequences become correct encodings of U+FFF8. func explode (s string, n int) []string { if n = = 0 { return nil (+ } L: = UTF8. Runecountinstring (s) if n <= 0 | | n > L { n = l + } A: = Make ([]string, N) var size, rune int i, cur: = 0, 0 i+1 < n; i++ { rune, size = UTF8. Decoderuneinstring (s[cur:]) a[i] = string (rune) cur + = size (+ } //Add the rest, if there is any if cur < len (s) { a[i] = s[cur:] * return a (+ }
|
Named
In Go the name not only has the function which expresses the meaning, but also has the characteristic which the restraint uses. If the name of a function is lowercase, it means that the function cannot be used in other packages.
The name must use the Camel name method, but not the underscore method.
Any name that needs to be exposed must start with capital letters, and names that do not need to be exposed outside the package must begin with a lowercase letter.
interface, as is customary, if the interface has only one method, the interface is named the method name plus an "ER" suffix.
Semicolon
Go and C language use ";" To end a statement, but not the same, in go by the compiler to handle ";", so you have to write the code is omitted ";". Of course there are exceptions , for loops ( used ; Distinguish between the initial part, the conditional part, and the traversal element, a row with multiple statements, multiple assignment statements, and so on.
It is important to note that the opening parenthesis of the control statement (for,if else,switch,select) cannot be set to another line. Such as
-
// the wrong way if (1==2) { FMT. Println ("god!") }
// correct writing if (1==2) { FMT. Println ("god!") } |
After learning new knowledge, Add.
---------2013-04-10 Supplement---------------------
Go Language Programming Specification:
Official address: http://golang.org/doc/effective_go.html (Wall)
Chinese translation: http://zh-golang.appsp0t.com/ref/spec?lang=en
------------------End------------------------------