1. variable
1.1 variables are defined using the keyword var. Variables are strongly typed.
Package main
Import "fmt"
Var I int
Var c, python, java bool
Func main (){
Fmt. Println (I, c, python, java)
}
1.2 When defining a variable, you can get the type by assigning values instead of specifying the type.
Package main
Import "fmt"
Var I, j int = 1, 2
Var c, python, java = true, false, "no! "
Func main (){
Fmt. Println (I, j, c, python, java)
}
1.3 define temporary variables in the function by: =
Package main
Import "fmt"
Func main (){
Var I, j int = 1, 2
K: = 3
C, python, java: = true, false, "no! "
Fmt. Println (I, j, k, c, python, java)
}
2. data type
2.1 all data types
Bool
String
Int int8 int16 int32 int64
Uint uint8 uint16 uint32 uint64 uintptr
Byte // alias for uint8
Rune // alias for int32
// Represents a Unicode code point
Float32 float64
Complex64 complex128
2.2 all data types must be converted. Otherwise, compilation errors may occur.
Var I int = 42
Var f float64 = float64 (I)
Var u uint = uint (f)
2.3 constants
A constant can be a number, string, character, or Boolean type.
The declaration of a constant is the same as that of a variable, but the const keyword is added before it.
Constants cannot be used: = value assignment
Package main
Import "fmt"
Const Pi = 3.14
Func main (){
Const World = "World"
Fmt. Println ("Hello", World)
Fmt. Println ("Happy", Pi, "Day ")
Const Truth = true
Fmt. Println ("Go rules? ", Truth)
}
3. struct
Struct is almost the same as the C language and is defined using the tpye and struct keywords.
Package main
Import "fmt"
Type Vertex struct {
X int
Y int
}
Func main (){
Fmt. Println (Vertex {1, 2 })
}
3.1 Use "point" to access the variables in the structure
Package main
Import "fmt"
Type Vertex struct {
X int
Y int
}
Func main (){
V: = Vertex {1, 2}
V. X = 4
Fmt. Println (v. X)
}
It is not perfect to have no struct in Java. Defining a struct requires writing a lot of code.
3.2 assign values to variable names during construction
Package main
Import "fmt"
Type Vertex struct {
X, Y int
}
Var (
P = Vertex {1, 2} // has type Vertex
Q = & Vertex {1, 2} // has type * Vertex
R = Vertex {X: 1} // Y: 0 is implicit
S = Vertex {} // X: 0 and Y: 0
)
Func main (){
Fmt. Println (p, q, r, s)
}