The structure of the body
Go constructs can contain multiple data types. Arrays in Go can contain only one data type. You can define a structure using type and struct:
type test struct { // Declare a structure named test
value int // Declare a field with the name value and data type int
name string // Declare one of the fields with the name name and data type string
}
Copy the code
The fields in the structure body are unique and cannot be duplicated. You can also place fields of the same type on a single line:
type letsgo struct{
data1,data2,data3 int
}
Copy the code
It is also possible to define an empty structure with no content:
type test strut{
}
Copy the code
Structure initialization
Defines a structure that will not be used and will not report an error, because a structure only specifies the memory layout of a set of data, and only instantiation allocates memory.
type test strut{
myvalue int
mydata string
}
var mytest test // Instantiate a struct, mytest is the struct instance, test is the struct type, this thing will be very common
Copy the code
A basic instantiation approach:
type letsgo struct{
data1,data2,data3 int
}
var p letsgo
p.data1 = 1
p.data2 = 2
p.data3 = 3
fmt.Println(p) {1,2,3}, {1,0,0}, {1,0,0}
Copy the code
Fields can be assigned at instantiation time, and must be in order:
type letsgo struct{
data1,data2,data3 int
}
var p =letsgo{1.30.3}
fmt.Println(p) / /,30,3 {1}
Copy the code
What if not in order:
type letsgo struct{
data1,data2,data3 int
}
var p =letsgo{data3:3,data2:30,data1:1}
fmt.Println(p) //{1,30,3}, again in the order of the structure output
Copy the code
Nested structures
Structures can also be nesting dolls, structures can also be inside a structure:
type father struct{
name string
other son
}
type son struct{
name string
}
Copy the code
Nested structures can also be initialized in the same way as above:
package main
import "fmt"
func main(a) {
type son struct{
name string
}
type father struct{
name string
other son //son must be defined above, otherwise an error will be reported.
}
p:=father{
name:"Zhang",
other:son{
name :"John's Son.",
},
}
fmt.Println(p) //{zhang SAN {Zhang SAN's son}}
fmt.Println(p.other.name) // Access the structure to use. Output: Zhang SAN's son
}
Copy the code
To access the structure, use the… example above. The second layer is also used.
Take the structure as a function parameter
To fill in the hole we dug when we were talking about functions, let’s go straight to an example:
package main
import "fmt"
type father struct{
name string
}
func printName ( fa father ){
fmt.Println(fa.name)
}
func main(a) {
p:=father{
name:"Zhang",
}
printName(p) // Run the following command
}
Copy the code
Try using a structure pointer as a function argument again:
package main
import "fmt"
type father struct{
name string
}
func printName ( fa *father ){
fmt.Println(fa.name) // Read the structure stored in the address of the passed FA, and print the fields in the structure
}
func main(a) {
p:=father{
name:"Zhang",
}
printName(&p) // pass the address of structure p
}
Copy the code
I’ve covered a lot of code examples here, and because I find that structure features are more straightforward to describe in code, the next article will look at interfaces that are closely associated with structures