Define a structure type
method: Type structure name struct{}
//定义一个person类
type person struct{
Name string
Age int
}
Structure initialization operation
a := person{
Name : "bai",
Age : 19,
}
Structure comparison method
If the members of the structure can be compared, then the structure can also be compared. The structure can be compared with == or !=. The comparison of the> <operator is not supported.
func main(){
a:=person{
"bai",19}
b:=person{
"zhao",20}
fmt.Println("a == b",a == b)
fmt.Println("a != b",a != b)
}
Assignment operations can be performed between structures of the same type.
a:=person{
"bai",19}
var temp person
temp = a
fmt.Println("a",a)
fmt.Println("temp",temp)
Structure as function parameter
- Value transfer: formal parameters cannot change actual parameters
func test1(p person){
p.Name = "zhao"
p.Age = 20
fmt.Println(p)
}
func main(){
a:=person{
"bai",19}
fmt.Println(a)
test1(a)
fmt.Println(a)
}
Value passing cannot change the actual parameter
- Address pass
func test1(p *person){
p.Name = "zhao"
p.Age = 20
fmt.Println(p)
}
func main(){
a:=person{
"bai",19}
fmt.Println(a)
test1(&a)
fmt.Println(a)
}
There is no class in the Go language, so there is no distinction between private members and shared members. Therefore, in the Go language, private and public are distinguished by the case of the first letter of the function.
- Capital letters indicate that the same package can also be accessed.
- Lowercase letters indicate that it can only be used in this file.