This is a creation in Article, where the information may have evolved or changed.
Names
Names is as important in Go as with any other language. In some cases they even has semantic effect:for instance, the visibility of a name outside a package are determined by WH Ether its first character was upper case. It ' s therefore worth spending a little time talking about naming conventions in Go programs.
Naming is important in all languages. In some cases, a name has a semantic effect, such as the case of the first letter of a name in a package to determine whether the name can be exported.
Package names
When a package is imported, the package name becomes an accessor for the contents. After
When the package is imported, the package is named an interface that accesses its internal namespace
Import "bytes"
The importing package can talk about bytes. Buffer. It's helpful if everyone using the package can use the same name to refer to its contents, which implies Name should be good:short, concise, evocative. By convention, packages is given lower case, single-word names; There should is no need for underscores or mixedcaps. Err on the side of brevity, since everyone using your package would be typing that name. And don ' t worry about collisions a priori . The package name was only the default name for imports; It need not being unique across all source code, and in the rare case of a collision the importing package can choose a Diffe Rent name to use locally. In any case, confusion are rare because the file name in the import determines just which package is being used.
After bytes is imported in the above code, we can use bytes directly. Buffer if each person using the package can refer to the contents of the package by the same name, that would be very good. This also means that the name of the package needs to be streamlined. A single word with the package name of lowercase usually does not need to be underlined or camel-like to give the wrap name without worrying about the package name conflict The package name is just the default name that is used when importing it does not need to be unique in all code if a name conflict occurs can be a local different name like import xxx as yyy in Python
Another convention is, the package name is the base name of the IT source directory; The package in Src/pkg/encoding/base64 are imported as "encoding/base64" but have name Base64, not encoding_base64 and not E NcodingBase64.
Another habit is that the package name is the directory name package in the source code structure src/pkg/encoding/base64 can be imported through "encoding/base64" instead of encoding_base64 or encodingBase64
The importer of a package would use the name to refer to its contents (the import notation are intended mostly for tests a nd other unusual situations and should is avoided unless necessary), so exported names in the "can use this" fact to Avoid stutter. For instance, the buffered reader type in the Bufio package is called Reader, Notbufreader, and because users see it as Bufio. Reader, which is a clear, concise name. Moreover, because imported entities is always addressed with their package name,bufio. Reader does not conflict with IO. Reader. Similarly, the function to make new instances of ring. Ring-which is the definition of aConstructorIn go-would normally was called newring, but since Ring was the only type of exported by the package, and since the package is Called ring, it ' s called just New, which clients of the package see asring. New. Use the package structure choose good names.
The benefit of having a package name to access its contents after the import of a packet is to reduce the parts of the name that have duplicate meanings, for example, if you do not need to use bufreader after importing the BUFIO package, you can use Bufio directly with reader. Reader's use of such a way to make people feel more simple and clear that the contents of the package is always accessed through Baonilai, so bufio. Reader will not and IO. The reader conflict creates the ring as well. The function of the ring may be defined in other languages as newring but the ring is the only type that is exported and the package name is ring. You can call the ring when the function name can be defined as new. New
Another short example is once. do; Once. Does (Setup) reads well and would is improved by writing once. Doorwaituntildone (Setup). Long names don ' t automatically make things more readable. If The name represents something intricate or subtle, it ' s usually better to write a helpful doc comment than to attempt t O Put all the information into the name.
Give another example once. do; Once. The Do (Setup) is a good naming method written in once. Doorwatiunitdone does not provide more information to understand the functionality of this function. Lengthy names do not make the code easier to read if the name needs to be complex or if it is not clear by a few words, it is best to add a document comment (code This is also recommended in complete)
Getters
Go doesn ' t provide automatic support for getters and setters. There ' s nothing wrong with providing getters and setters yourself, and it's often appropriate to doing so, but it's neither I Diomatic nor necessary to put Get into the Getter ' s name. If you had a field called owner (lower case, unexported), the getter method should is called owner (upper case, exported) , not GetOwner. The use of Upper-case names for export provides the hook to discriminate the field from the method. A setter function, if needed, would likely be called SetOwner. Both names read well in practice:
Go does not provide setter and getter support but it doesn't hurt if you want to write it, and it's always recommended that you do it, but you don't need a get in the name of the function. If there is a field that is owner, the name of the getter can be defined directly as owner Do not getowner such a name look at the awkward initials capitalized as an exportable logo a little conspicuous ha and the other differences open if a setter function is required this can be defined as SetOwner
Owner: = obj. Owner () if owner! = user { obj. SetOwner (User)}
Interface names Interface naming
By convention, One-method interfaces is named by the method name plus The-er Suffix:reader, Writer, Formatter etc.
The interface that is used to include only one method when naming at the end plus er as a suffix such as: Reader Writer Formatter, etc.
There is a number of such names and it's productive to honor them and the function names they capture. Read, Write, Close, Flush, String and so on has canonical signatures and meanings. To avoid confusion, don ' t give your method one of the those names unless it has the same signature and meaning. Conversely, if your type implements a method with the same meaning as a method on a well-known type, give it the same name and signature; Call your String-converter method string nottostring.
Examples of using this rule name many Read Write Close Flush string and so on have their typical declaration characteristics and meanings By contrast if your custom type implements a type method that everyone recognizes, give it a definition of the same name and declaration, such as converting your type to a string type Your conversion function is named string instead of ToString.
Mixedcaps
Finally, the Convention in Go are to use mixedcaps or mixedcaps rather than underscores to write multiword names.
The last go uses camel-named notation without underlining the way
Semicolons
Like C, Go ' s formal grammar uses semicolons to terminate statements; Unlike C, those semicolons does not appear in the source. Instead The lexer uses a simple rule to insert semicolons automatically as it scans, so the-input text is mostly free of t Hem.
Same as C go use semicolon concluding sentence but there are also different from C in the go code is usually not seen in the semicolon lexical analyzer when parsing source code using simple rules to automatically insert semicolons
The rule is this. If the last token before a newline be an identifier (which includes words like int and float64), a basic literal such as a Number or string constant, or one of the tokens
If a token before the newline character is an identifier (int float64 stream), a number or string constant, or the following tokens:
Break continue Fallthrough return + +--)}
The lexer always inserts a semicolon after the token. This could is summarized as, "if the newline comes after a tokens that could end a statement, insert a semicolon".
The lexical analyzer adds a semicolon after the token.
A semicolon can also is omitted immediately before a closing brace, so a statement such as
The semicolon immediately following the closing parenthesis can be omitted, as in the following example:
Go func () {for {DST <-<-src}} ()
Needs no semicolons. Idiomatic Go programs has semicolons only on places such as for loop clauses, to separate the initializer, condition, and Continuation elements. They is also necessary to separate multiple statements on a line, and should you write code the.
Usually there is only a for loop where semicolons are used in the Go program in order to differentiate between the initial and subsequent elements if there are multiple statements on the same line then you need to use a semicolon but a line.
One caveat. You should never put the opening brace of a control structure (if, for, switch, or select) on the next line. If you do, a semicolon'll be inserted before the brace, which could cause unwanted effects. Write them like this
Remind me never to put the starting parenthesis on the next line (parentheses in the IF for switch SELECT statement) If you do this, the lexical analyzer inserts a semicolon in front of the parentheses.
If I < F () { g ()}
The following is a wrong notation
If I < f () //wrong!{ wrong! g ()}