struct structs are similar to class classes in the Python language, and the elements of a struct class can be a variable, or a function or other type, like the properties and methods of a python.
// struct结构体,类似python语言中的class类package mainimport "fmt"type person struct { //定义一个strcut Name string Age int}func main() { a := person{} //赋值给一个变量a fmt.Println(a) a.Name = "faily" //为结构体赋值 a.Age = 28 fmt.Println(a)}/*输出结果 { 0} //空结构体时,int元素为0{faily 28}*/
Simple way to initialize a struct
package mainimport "fmt"type person struct { //定义一个strcut Name string Age int}func main() { a := person{ //也可以这样初始化一个结构体 Name: "msy", Age: 27, } fmt.Println(a)}
- The
-
Struct is also a value type
, which means that when passed, the copy is a value, the normal path cannot change the value within the structure, and it needs to be modified by pointer
package Mainimport ' fmt ' type person struct {//define a strcut Name string age Int}func main () {a: = person{/ /Initialize struct Name: "MSY", Age:27,} fmt. Println ("1", a) A (a) fmt. Println ("3", a) B (&a)//pointer refers to FMT. Println ("4", a)}func a (per person) {///parameter references person--struct per. Age = 13//Initializes the value of the person property to FMT. Println ("2", per)}func B (per *person) {//pass pointer per. Age =13 FMT. Println ("2", per)}/* output initialization: 1 {MSY 27} Modification of A:2 {MSY 13} modification failed: 3 ' {MSY 27} modified B:2 {MSY 13} modified successfully: 3 "{MSY 13 } */
It is more convenient to modify the structure by using the structure pointer when it is recommended to initialize the struct body.
package mainimport "fmt"type person struct { //定义一个strcut Name string Age int}func main() { a := &person{ //初始化结构体,建议使用结构的指针 Name: "msy", Age: 27, } a.Name = "MYF" //这种方式就可以直接修改结构体 fmt.Println("1", a) A(a) //此时就可以直接修改结构体 B(a) fmt.Println("3", a)}func A(per *person) { //传递指针 per.Age = 13 //初始化person属性的值 fmt.Println("2", per)}func B(per *person) { //传递指针 per.Age = 100 fmt.Println("2", per)}/*输出1 &{MYF 27}2 &{MYF 13}2 &{MYF 100}3 &{MYF 100}*/
Anonymous structure: Structure without a name
package mainimport "fmt"func main() { a := &struct { //指向结构体指针 Name string Age int }{ //直接初始化 Name: "moshaoying", Age: 27, } fmt。Println(a)}/*输出&{moshaoying 27}*/
-
Nested anonymous struct, initialization mode
package Mainimport "FMT" type person struct {//nested anonymous struct Name string A GE int Content struct {Phone int Addr string}}func main () {A: = Person{name: "Moshaoying", age:22} A.content.phone = 13636269334//nested anonymous struct initialization mode a.content.addr = "Shanxi,xian" FMT. Println (a)}/* output {moshaoying {13636269334 shanxi,xian}}*/
-
Struct-body comparison: Naming is part of a struct, so you can compare it only if the struct is named the
package Mainimport "FMT" type person struct {/ /nested anonymous struct name string age int}type person1 struct {Name string Age Int}func main () {A: = Person{name: "Mo Shaoying ", age:22} B: = Person1{name:" Moshaoying ", age:22} c: = Person{name:" Moshaoying ", age:22} D: = per Son{name: "Moshaoying", age:21} fmt. Println (A = = b) fmt. Println (A = = c) fmt. Println (A = = d)}/* output error a=b Invalid operation:a = B (mismatched types person and Person1) a=c Truea=d false*/
- The
-
Struct does not inherit from the Python class, but the struct also has a similar function, nested within the struct, to inherit the properties of the other struct
package Mainimport "FMT" type Human struct {Sex int}type person struct {//struct inherits (nested) human Name string Age Int}func main () {A: = Perso N{name: "Failymao", Age:22, human:human{sex:250}}//The first way, note the principle of FMT. Println (a)//second Way a.name = "Allen" a.age = A.sex = 222 FMT. Println (a)}/* output error First way: {{22} Failymao} The second way: {{222} Allen 29}*/