This is a creation in Article, where the information may have evolved or changed.
Go Web Programming "go language Programming" reading notes.
1. Variables
Variables are the most basic constituent elements in almost all programming languages. Essentially, a variable is a name for a piece of data storage space, and the program can request a chunk of data storage by defining a variable, and then use that storage space by referencing the variable name.
1.1 Variable Declaration
For purely variable declarations, the Go language introduces the keyword VAR, and the type information is placed after the variable name, such as:
typetype
Example:
varV1intvarV2stringvarV3[Ten]int //ArrayvarV4 []int //array slicesvarV5struct{Fint}varV6 *int //HandsvarV7Map[string]int //Map,key is a string type and value is of type intvarV8func(Aint)int
- Brief statement
: = This symbol can replace the var keyword and the variable type directly, this form can only be used inside the function, and the left variable should not be declared. Shaped like:
vname:=value
Example:
v3 := 10 //编译器可以自动推导出v3的类型
In the go language, when declaring multiple constants, variables, or importing multiple packages, you can declare them in a grouped way.
var ( int string)
1.2 Variable initialization
For a scene that needs to be initialized when declaring a variable, the specified type is no longer required, and the go compiler can deduce the variable type from the right value of the initialization expression.
Shaped like:
var vname type=valuevar vname =value//自动推导变量类型var vname1, vname2, vname3 type= value1, value2, value3var//自动推导变量类型
Example:
varint10// 正确的使用方式1var10// 正确的使用方式2,编译器可以自动推导出v2的类型
1.3 Assigning values to variables
In go syntax, variable initialization and variable assignment are two different concepts.
Declare a variable and assign a value:
varint123
The variable assignment of the Go language is consistent with most languages, but supports multiple assignments , example:
v1,v2 = 1,2
1.4 Anonymous variables
_ (underscore) is a special variable name, and any value given to it will be discarded. Example:
_ , b := 34, 35
TIP:
- Go error in compile phase for declared but unused variables
- A variable that starts with a capital letter is a public variable, and the lowercase letter begins with a private variable.
- A function that starts with a capital letter is the same as a public function with a common keyword in class, and a private function that starts with a secret keyword.
2. Constants
A constant is a value that is determined at the time of the program's compilation, and the program cannot change the value while it is running. In Go programs, constants can be numeric types (including integer, float, and complex types), Boolean types, string types, and so on.
2.1 Defining constants
By using the Const keyword, you can define a constant with the following syntax
const constantName [type]=value
The constant definition of go can qualify a constant type, but is not required. If you define a constant without specifying a type, then it is an untyped constant. As long as the constant is within the range of the corresponding type, it can be used as a constant of that type, such as constant-12, which can be assigned to variables of type int, uint, Int32, Int64, float32, float64, complex64, complex128, etc.
Example:
const Pi float64 =< Span class= "Hljs-number" > 3.14159265358979323846 const zero = 0.0 //no type floating-point constant const (size int64 = 1024x768 eof =-1 //untyped integer constant ) const u, v float32 = 0 , 3 //u = 0.0, v = 3.0, constant multiple assignment const A, B, C = 3 , 4 , " foo " //a = 3, B = 4, c = "foo", untyped integer and string constant
The right-hand value of a constant definition can also be a constant expression that is evaluated at compile time, such as
Const MASK = 1 << 3
Because the assignment of a constant is a compile-time behavior, an rvalue cannot have any expression that requires a run time to produce a result, such as attempting to define a constant in such a way as to cause a compilation error:
const Home = os.GetEnv("HOME")
The reason is simple, os. GETENV () The return result is known only at run time and is not determined at compile time, so
The right value cannot be defined as a constant.
2.2 Pre-defined constants
The Go language pre-defines these constants: True, False, and iota.
Iota is special and can be thought of as a constant that can be modified by the compiler, which has a default starting value of 0, plus 1 for each call. Reset to 0 when the Const keyword is encountered.
Example:
Const(//Iota reset to 0C0 =Iota //C0 = = 0C1 =Iota //C1 = = 1C2 =Iota //C2 = = 2)Const(A =1<<Iota //A = = 1 (iota is reset to 0 at the beginning of each const)b =1<<Iota //b = = 2c =1<<Iota //c = = 4)Const(U =Iota* the //U = = 0Vfloat64=Iota* the //V = = 42.0W =Iota* the //W = =)Constx =Iota //X = = 0 (because iota is reset to 0)Consty =Iota //Y = = 0 (ibid.)
If the expression of the two Const assignment statement is the same, then the latter assignment expression can be omitted. Therefore, the first two const statements above can be abbreviated as:
const ( // iota被重设为0 iota// c0 == 0 c1 // c1 == 1 c2 // c2 == 2)const ( a = 1 <<iota// a == 1 (iota在每个const开头被重设为0) b // b == 2 c // c == 4)
2.3 Enumeration
Enumeration refers to a series of related constants. The go language does not support enum keywords that are explicitly supported in many other languages. Defines a set of constants in the form of a const followed by a pair of parentheses, which is typically used to define enumeration values in the Go language.
For example, for the definition of daily in one weeks, here is a general enumeration notation, which defines a series of integer constants:
const ( Sunday = iota Monday Tuesday Wednesday Thursday Friday Saturday numberOfDays // 这个常量没有导出)
Copyright Notice: