4.1. Structure
Structure: one or more variables in combination with the formation of a new type, this is the type of structure, the structure is a value type
Definition of the structure and assignment
//Learn_Go/main.go package main import "fmt" type People struct { Name string Age int } func main() { var peo People fmt.Println(peo) //{ 0} fmt.Printf("%p",&peo) //0x110040f0 // assignment //The first peo = People{"derek",20} fmt.Println(peo) //{derek 20} // The second peo2 := People{Age:12,Name:"jack"} fmt.Println(peo2) //{jack 12} // third peo.Name = "alice" peo.Age = 25 fmt.Println(peo) //{alice 25} }
4.2. Structure pointer
Since the structure is a value type, when the transfer method of transfer desired address structure can be used to complete a structure pointer
May be combined with new () function creates a structure pointer
//Learn_Go/main.go package main import "fmt" type People struct { Name string Age int } func main() { peo := new(People) fmt.Println(peo) //&{ 0} fmt.Println(peo == nil) //false peo.Name = "derek" peo.Age = 22 fmt.Println(peo) //&{derek 22} peo2: = peo fmt.Println(peo2) //&{derek 22} peo2.Name = "Jack" fmt.Println(peo, peo2) //&{Jack 22} &{Jack 22} }
4.3. Methods
Methods and functions more like syntax, except that the function package belongs, by calling the function package, the method belongs to the structure, the structure variable by calling
The default is a function, under the package, so it is necessary to label, which tells the compiler that this method belongs to the structure
(1) creation method
//Learn_Go/main.go package main import "fmt" type People struct { Name string Weight int } func (p People) run() { fmt.Println (p.Name, "is running, the current weight is:", p.Weight) } func main() { peo := People{"derek",120} peo.run () // derek is running, the current body weight: 120 }
(2) modify the value of pointer
//Learn_Go/main.go package main import "fmt" type People struct { Name string Weight int } func (p *People) run() { fmt.Println (p.Name, "is running, the current weight is:", p.Weight) // run once run method, body weight minus 1 p.Weight -= 1 } func main() { peo := People{"derek",120} peo.run () // derek is running, the current body weight: 120 fmt.Println ( "step after the finish of body weight:", peo.Weight) // step after the finish of body weight: 119 }
Source Address: https://www.cnblogs.com/derek1184405959/
4.1. Structure
Structure: one or more variables in combination with the formation of a new type, this is the type of structure, the structure is a value type
Definition of the structure and assignment
//Learn_Go/main.go package main import "fmt" type People struct { Name string Age int } func main() { var peo People fmt.Println(peo) //{ 0} fmt.Printf("%p",&peo) //0x110040f0 // assignment //The first peo = People{"derek",20} fmt.Println(peo) //{derek 20} // The second peo2 := People{Age:12,Name:"jack"} fmt.Println(peo2) //{jack 12} // third peo.Name = "alice" peo.Age = 25 fmt.Println(peo) //{alice 25} }
4.2. Structure pointer
Since the structure is a value type, when the transfer method of transfer desired address structure can be used to complete a structure pointer
May be combined with new () function creates a structure pointer
//Learn_Go/main.go package main import "fmt" type People struct { Name string Age int } func main() { peo := new(People) fmt.Println(peo) //&{ 0} fmt.Println(peo == nil) //false peo.Name = "derek" peo.Age = 22 fmt.Println(peo) //&{derek 22} peo2: = peo fmt.Println(peo2) //&{derek 22} peo2.Name = "Jack" fmt.Println(peo, peo2) //&{Jack 22} &{Jack 22} }
4.3. Methods
Methods and functions more like syntax, except that the function package belongs, by calling the function package, the method belongs to the structure, the structure variable by calling
The default is a function, under the package, so it is necessary to label, which tells the compiler that this method belongs to the structure
(1) creation method
//Learn_Go/main.go package main import "fmt" type People struct { Name string Weight int } func (p People) run() { fmt.Println (p.Name, "is running, the current weight is:", p.Weight) } func main() { peo := People{"derek",120} peo.run () // derek is running, the current body weight: 120 }
(2) modify the value of pointer
//Learn_Go/main.go package main import "fmt" type People struct { Name string Weight int } func (p *People) run() { fmt.Println (p.Name, "is running, the current weight is:", p.Weight) // run once run method, body weight minus 1 p.Weight -= 1 } func main() { peo := People{"derek",120} peo.run () // derek is running, the current body weight: 120 fmt.Println ( "step after the finish of body weight:", peo.Weight) // step after the finish of body weight: 119 }