1. The name
All naming in Go follows the following rules:
- A name must begin with a letter (Unicode letter) or underscore, and can be followed by any number of letters, numbers, or underscores
- Case sensitive
- Keywords in Go cannot be used for naming
- It is recommended to usecamelName, but some specific words should be avoided in case writing, such as HTML, can be named as
htmlEscape
orHTMLEscape
But don’t beHtmlEscape
2. Key words
There are 25 keywords like if and switch in Go; Keywords cannot be used for custom names and can only be used in specific syntax structures.
break default func interface select
case defer go map struct
chan else goto package switch
const fallthrough if range type
continue for import return var
Copy the code
3. Built-in constants/types/functions
Internally predefined constants, types, functions:
Built-in constants:true false iota nilBuilt in types:int int8 int16 int32 int64
uint uint8 uint16 uint32 uint64 uintptr
float32 float64 complex128 complex64
bool byte rune stringError built-in function:make len cap new append copy close delete
complex real imag
panic recover
Copy the code
In particular, Golang has three built-in reference types, often referred to as Pointers:
slice // Sequence array
map
chan / / piping
Copy the code
4. The scope
Before we talk about scopes, let’s define a few concepts using the following examples:
package main
import "fmt"
var arr = [...]int{1.2.3.4.5.6}
func traverseArray(arr [6]int) {
a := 11
fmt.Println(a)
for i, v := range arr {
b := 22
fmt.Println(b)
fmt.Println("index:",i ,"value",v)
}
}
func main(a) {
traverseArray(arr)
}
Copy the code
The scope in Go depends on the lexical domain
- 2. A region, or scope, in which a variable is in effect.
The lexical domain is divided into explicit and implicit
- Display lexical scope: parentheses
{}
A block containing a series of statements. Names declared inside a block cannot be accessed by an external block. This block determines the scope of the name of the internal declaration, i.e., scope.traverseArray
with{}
The wrapped function body is the variable declared in an explicit lexical fielda
, can only be used inside a function, and cannot be accessed outside the functionfor i, v := range arr
with{}
The wrapped loop body is also the variable declared in the lexical domainb
Can only be used in the body of a loop
- Implicit lexical domain: not used
{}
Parcel part- All code, in this case, corresponds to the corresponding scope of built-in functions, built-in types, etc., calledBuilt-in scope
- Such as
int
,len()
Can be used and accessed in any code
- Such as
- Package statement block: contains all the source code in the package (a package may contain multiple files in a directory), corresponding toPackage-level scope
- Global variables here
arr
Belongs to the package statement block,traverseArray
No, but if you capitalize the first letter of its function name, i.eTraverseArray
Indicates that the method can be accessed by other files within the package, also known as package block
- Global variables here
- File statement block: contains all source code in the file, correspondingFile-level scope
traverseArray
It can only be accessed in the source file
For, if, switch
, corresponding toLocal scope- Here,
for
A variable declared in a loopi,v
That is, the corresponding local scope can only be infor
The use of
- Here,
- All code, in this case, corresponds to the corresponding scope of built-in functions, built-in types, etc., calledBuilt-in scope
Four kinds of scope understanding
The above four scopes, from top to bottom, range from large to small, for the sake of expression, I myself here called the large scope of the high-level scope, and the small scope called the low-level scope.
For scopes, the following points are summarized:
- Lower level scope, with access to higher level scope
- Scopes at the same level are isolated from each other
- Variables declared in the lower level scope override variables declared in the higher level scope
Be careful not to confuse scope and life cycle here. The scope of the declaration statement corresponds to a text area of the source code; It is a compile-time property.
The lifetime of a variable refers to the period of time during which a variable exists in the program, within which it can be referenced by other parts of the program. Is a runtime concept.
Reference documentation
The Go language bible: books.studygolang.com/gopl-zh/
Studygolang.com/articles/12…