1. Notes
Comments in Go language are mainly divided into two categories, namely single-line comments and multi-line comments.
1.1. Single line comments
//
Single-line comments, referred to as line comments, are the most common form of comments. Single-line comments starting with ; can be used anywhere ;
// 单行注释
1.2. Multi-line comments
Multi-line comments, referred to as block comments, /*
begin with and */
end with, and cannot be nested. Multi-line comments are generally used for package documentation descriptions or code snippets commented into blocks.
/*
这是多行注释
可以跨越多行
*/
1.3. Note usage scenarios
- Explain the functions and parameters of functions and methods to improve code readability and maintainability.
// Add 函数将两个整数相加,并返回和
func Add(a, b int) int {
return a + b
}
- Mark the author, time, version and other information of the code to facilitate code management and maintenance.
/*
Author: John
Date: 2021-10-01
Version: 1.0
*/
- Disable or debug code segments to facilitate troubleshooting bugs and testing code.
// TODO: implement this function
func MyFunction() {
// debug info
fmt.Println("Debug info...")
}
2. Keywords
Keywords, also called reserved words.
There are a total of 25 keywords in Go language:
break | default | func | interface | select |
---|---|---|---|---|
case | defer | go | map | struct |
chan | else | goto | package | switch |
const | fallthrough | if | range | type |
continue | for | import | return | was |
Like other languages, keywords cannot be used as identifiers.
2. Identifier
In the Go language, an identifier is a name used to identify a program entity (such as a variable, function, structure, etc.). The identifier consists of several letters, underscore_, and numbers, and the first character must be a letter. In layman's terms, any name that can be defined by itself can be called an identifier.
2.1. Identifier naming rules
- The identifier consists of letters, numbers, and underscores (_), and cannot start with a number.
- Identifiers are case-sensitive, for example Foo and foo are two different identifiers.
- You cannot use Go language keywords as identifiers.
- Identifiers should be descriptive and express their meaning concisely and clearly.
- Use camel case, that is, the first letter is lowercase, and the first letter of each subsequent word is capitalized, such as myName, myAge, etc.
- For private variables and functions in the package, you can use an underscore (_) as a prefix, such as _privateFunction, _privateVariable.
- Identifier cannot contain spaces
- It cannot be the same as the package name in the standard library.
- The naming of identifiers should be as short and meaningful as possible
2.2. Blank identifier
In Go, _
(underscore) is called a "whitespace identifier" and can be used as a placeholder or to ignore a value. The usage scenarios are as follows:
- Ignore the value of a variable
// 仅获取文件信息,忽略错误
info, _ := os.Stat("example.txt")
- Ignore packages in import statements
import (
"fmt"
_ "net/http/pprof" // 忽略该包
)
- Ignore function return values or parameters
// 忽略函数的返回值
_, err := doSomething()
// 忽略函数的参数
func doSomething(_, b int) {
// ...
It should be noted that blank identifiers can only be used as placeholders or to omit a certain value, that is, any value assigned to this identifier will be discarded, so these values cannot be used in subsequent code, nor can Use _
as a variable to assign or perform operations on other variables.
2.3. Differences in capitalization and capitalization of initial letters
In the Go language, the first letters of identifiers (variable names, function names, constant names, etc.) have different meanings and effects.
- An identifier with a capital letter indicates that the identifier is exported (public) and can be accessed and used by other packages.
- An identifier with the first letter lowercase indicates that the identifier is unexported (private) and can only be used in the current package.
2.4. Predefined identifiers
There are 36 predefined identifiers in total, which mainly include basic data types and built-in functions in the Go language. These predefined identifiers cannot be used as identifiers.
append | bool | byte | cap | close | complex |
---|---|---|---|---|---|
complex64 | complex128 | uint16 | copy | false | float32 |
float64 | imag | int | you8 | int16 | uint32 |
int32 | int64 | iota | len | make | new |
nil | panic | uint64 | println | real | |
recover | string | true | uint | uint8 | uintptr |
3. Priority of operators
Operators are used to perform mathematical or logical operations when the program is running. The so-called priority refers to which operator is executed first when multiple operators appear in the same expression.
The following table is a detailed table of operator priority, and the priority is getting lower and lower from top to bottom
Classification | operator | associativity |
---|---|---|
postfix operator | ( ) 、[ ] 、-> |
from left to right |
Unary operator | ! ,* (pointer), & , ++ , -- , + (positive sign), - (negative sign) |
right to left |
Multiplication/division/remainder | * (multiplication sign), / ,% |
from left to right |
Addition/Subtraction | + 、- |
from left to right |
Bit shift operator | << 、>> |
from left to right |
Relational operators | < 、<= 、> 、>= |
from left to right |
equal/not equal | == 、!= |
from left to right |
bitwise AND | & |
from left to right |
bitwise XOR | ^ |
from left to right |
bitwise or | | |
from left to right |
logic and | && |
from left to right |
logical or | || |
from left to right |
assignment operator | = 、+= 、-= 、*= 、/= 、 %= 、 >= 、 <<= 、&= 、^= 、|= |
right to left |
comma operator | , |
from left to right |
Reference document
1. http://c.biancheng.net/view/5559.html