In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly analyzes the relevant knowledge points of variable naming rules and case analysis in Go, the content is detailed and easy to understand, the operation details are reasonable, and has a certain reference value. If you are interested, you might as well follow the editor to have a look, and follow the editor to learn more about "variable naming rules and case analysis in Go".
Naming habits are important.
Good readability is one of the requirements of high quality code.
Good naming habits help to improve code readability
The quality of being well named and liked.
Good Name will have the following characteristics:
Consistent (easy to guess)
Short (easy to type)
Accurate (easy to understand)
Rule of thumb
The farther away the variable declaration is from the variable usage, the longer the variable name should be.
This also explains why the for loop number variable uses I as the temporary variable for traversal rather than the semantically more specific index as the variable name.
Variable name in mixed case
We should not use names_with_underscores as the variable name, but namesWithUnderscores as the variable name.
Acronyms, on the other hand, should be capitalized, such as ServeHTTP and IDProcessor.
This is called MixedCase, similar to the hump principle, but the initials of professional words should be capitalized.
Variable names avoid redundancy
The variable name is not as long as possible, the constant variable name will obscure the code function.
Common combinations of constants and types may use very short names:
Use I instead of index
Use r instead of reader
Use b instead of buffer
Avoid using redundant names, depending on the context:
Inside the RuneCount method, use count instead of reuneCount
In the map statement, use ok instead of keyInMap:
V, ok: = m [k]
Constant variable names may be helpful in long functions and functions with many variables, but this usually means that you should ReFactor the code.
Bad codes vs good codes
/ / Badfunc RuneCount (buffer [] byte) int {/ / runeCount-> count runeCount: = 0 / / index-> I, buffer-> b for index: = 0; index
< len(buffer); { if buffer[index] < RuneSelf { index++ } else { // size ->N _, size: = DecodeRune (buffer [index:]) index + = size} runeCount++} return runeCount} / / Goodfunc RuneCount (b [] byte) int {count: = 0 for I: = 0; I < len (b) {if b [I] < RuneSelf {ionization +} else {_, n: = DecodeRune (b [I:]) I + = n} count++} return count} function parameter naming
Function parameters, like variable names, act as documents.
1. When the type of a function parameter is descriptive, the function parameter name can be shorter:
Func AfterFunc (d Duration, f func ()) * Timerfunc Escape (w io.Writer, s [] byte)
two。 When the type semantics of function parameters are unclear, the parameter names should be more specific and detailed:
Naming of the return value of func Unix (sec, nsec int64) Timefunc HasPrefix (s, prefix [] byte) bool
The return value of the export function (exported function) should be named only for documentation purposes.
The following is a good example of a return value naming:
/ / naming of the Goodfunc Copy (dst Writer, src Reader) (written int64, err error) / / Goodfunc ScanBytes (data [] byte, atEOF bool) (advance int, token [] byte, err error) method Receiver
A construct in Go can have a method, and when a method is declared for a structure, the structure is called receiver.
By convention, the name of the method recipient is usually 1 or 2 characters, because each method of the structure will use the same receiver name.
/ / Goodfunc (b * Buffer) Read (p [] byte) (n int, err error) / / Goodfunc (sh serverHandler) ServeHTTP (rw ResponseWriter, req * Request) / / Goodfunc (r Rectangle) Size () Point
The name of the Receiver must be consistent, and if the method1 of the structure uses r as the receiver name, then the method2 should not use rdr as the name.
Export package level variable naming
Variables at the package level are already qualified by package name, so you need to pay attention to the redundancy of derived variables, constants, functions, and type names.
For example:
We use bytes.Buffer instead of bytes.ByteBuffer
We use strings.Reader instead of strings.StringReader
Export variable names do not have redundancy with package.
Interface Typ
There is only one method interface, and the interface name is usually simple to add er to the method to issue commands, for example:
Type Reader interface {Read (p [] byte) (n int, err error)}
Sometimes, the above strategy will result in incorrect syntax of the interface name, but we can still choose to do so, for example:
Type Execer interface {Exec (query string, args [] Value) (Result, error)}
Sometimes, we modify the interface name to make it conform to English grammar:
Type ByteReader interface {ReadByte () (c byte, err error)}
When an interface contains multiple methods, you should choose a name that accurately describes its purpose, such as net.Conn,http.ResponseWriter,io.ReadWriter.
Error naming
Error types and error variables should have different naming formats:
/ / error type Error typestype ExitError struct {.} / / error variable Error valuesvar ErrFormat = errors.New ("image: unknown format") Packages name
Select the package name that is meaningful to the exported name.
Avoid using package names such as util, common, etc.
Conclusion
Use short variable names
Variable names take into account the context to avoid redundancy, such as local variables in the function consider the function name, package derived variables consider the package name
What is gogo is the abbreviation of golang? golang is a static strongly typed, compiled, concurrent and garbage-collected programming language developed by Google. Its syntax is similar to C language, but it does not include functions such as enumeration, exception handling, inheritance, generics, assertions, virtual functions and so on.
On the "Go variable naming rules and case analysis" is introduced here, more related content can search previous articles, hope to help you answer questions, please support the website!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.