This is a creation in Article, where the information may have evolved or changed.
struct struct
- The struct in Go is very similar to the struct in C, and go does not have a class, instead of the class position, but does not replace the function of class
- Use the Type
- Supports pointer-type members pointing to themselves
- Supports anonymous structures that can be used as members or define member variables
- Anonymous structures can also be used for map values
- Structure can be initialized with literal values
- Allows you to read and write struct members directly through pointers
- Members of the same type can perform direct copy assignment
- The = = and! = Comparison operators are supported, but not > or <
- Support for anonymous fields, essentially a field defined by a type name
- Embedded structure as anonymous field looks like inheritance, but not inherited
- You can use the anonymous field pointer
Definition of structure
package mainimport ( "fmt")type Person struct { //结构也是一中类型 Name string //定义struct的属性 Age int}func main() { a := Person{} a.Name = "joe" //对struct的属性进行操作,类型与class的使用方法 a.Age = 19 fmt.Println(a)}
Literal initialization
package mainimport ( "fmt")type Person struct { Name string Age int}func main() { a := Person{ Name: "jack", Age: 19, //对结构的属性进行字面值的初始化 } fmt.Println(a)}
Transfer of structures
package mainimport ( "fmt")type Person struct { Name string Age int}func main() { a := Person{ Name: "jack", Age: 19, //对结构的属性进行字面值的初始化 } fmt.Println(a) A(a) fmt.Println(a) //结构也是一种值类型,对它进行传递的时候,也是进行了值得拷贝}func A(per Person) { per.Age = 13 fmt.Println("A", per)}PS G:\mygo\src\mytest> go run .\temp.go{jack 19}A {jack 13}{jack 19}
Package Mainimport ("FMT") type person struct {Name string Age Int}func main () {A: = person{ Name: "Jack", age:19,//Initialize the property of the structure with literal value} FMT. Println (a) A (&a) fmt. Println (a)///structure is also a value type, when it is passed, it is also worth copying}func a (per *person) {//pass through a pointer, it is not worth copying the per. Age = Fmt. Println ("A", per)}ps g:\mygo\src\mytest> go run. \temp.go{jack 19}a &{jack 13}{jack)
package mainimport ( "fmt")type Person struct { Name string Age int}func main() { a := &Person{ Name: "jack", Age: 19, //此时初始化的时候就将这个struct的指针取出来 } //在进行struct的初始化的时候,就加上&取地址符号 fmt.Println(a) A(a) B(a) fmt.Println(a) //结构也是一种值类型,对它进行传递的时候,也是进行了值得拷贝}func A(per *Person) { //通过一个指针进行传递,此时就不是值得拷贝了 per.Age = 13 fmt.Println("A", per)}func B(per *Person) { //通过一个指针进行传递,此时就不是值得拷贝了 per.Age = 15 fmt.Println("B", per)}PS G:\mygo\src\mytest> go run .\temp.go&{jack 19}A &{jack 13}B &{jack 15}&{jack 15}
Anonymous structure
package mainimport ( "fmt")func main() { a := &struct { //匿名结构,需要先对结构本身进行一个定义 Name string Age int }{ Name: "jack", Age: 20, } fmt.Println(a)}
Nesting of anonymous structures
package mainimport ( "fmt")type Person struct { Name string Age int Contact struct { Phone, City string //匿名结构嵌套在Person中 }}func main() { a := Person{Name: "Jack", Age: 20} a.Contact.Phone = "123321" //通过这种方法对嵌套在Person中的匿名结构进行字面值的初始化 a.Contact.City = "BeiJing" fmt.Println(a)}PS G:\mygo\src\mytest> go run .\temp2.go{Jack 20 {123321 BeiJing}}
Anonymous fields
package mainimport ( "fmt")type Person struct { string //匿名字段 在进行字面值初始化的时候 必须严格按照字段声明的顺序 int}func main() { a := Person{"Jack", 20} //此时将string 和 int类型对调的时候就会报错 fmt.Println(a)}
Comparison of the same type of structure
package mainimport ( "fmt")type Person struct { Name string Age int}func main() { a := Person{Name: "Jack", Age: 20} b := Person{Name: "Jack", Age: 20} fmt.Println(a == b)}PS G:\mygo\src\mytest> go run .\temp3.gotrue
Embedding structure
package mainimport ( "fmt")type human struct { Sex int}type teacher struct { human Name string Age int}type student struct { human //这里的human也是一种类型,此时它相当于一种匿名字段,嵌入结构作为匿名字段的话 //它本质上是将结构名称作为我们的字段名称 Name string Age int}func main() { a := teacher{Name: "Jack", Age: 20, human: human{Sex: 0}} //因此我们需要在这里进行这种初始化 b := student{Name: "Tom", Age: 19, human: human{Sex: 1}} a.Name = "Fack" a.Age = 13 a.human.Sex = 100 //保留这种调用的方法,是因为会涉及到名称的冲突 //a.Sex = 101 这种写法也是可以的 fmt.Println(a, b)}PS G:\mygo\src\mytest> go run .\temp3.go{{100} Fack 13} {{1} Tom 19}