struct
Go is not a pure object-oriented programming language. Go uses a more flexible structure instead of a class.
Go does not provide classes, but it does provide structs on which methods can be added. Similar to classes, constructs provide the behavior of binding data and methods.
Go language object oriented main learning structure (struct), method (method), interface (interface)
1.1 Definition of structure
Struct {member attribute 1 type 1 Member attribute 2 Type 2 Member attribute 3 type 3}Copy the code
Instantiate the Sample structure
Package main import "FMT" type Teacher struct {name string age int sex byte} func main() {// Object. Property = Value var t1 Teacher fmt.Println(t1) fmt.Printf("%T\n,%v\n,%q \n",t1,t1,t1) t1.name="JackMa" t1.age=10086 t1.sex=1 FMT. Println (t1) FMT. Println (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ") / / variable brief statement format instantiated structure, initialization method is: the object. Attribute = value t2: = the Teacher {} t2. The name = "pink" t2. Age = 132 t2. Sex = 1 FMT. Println (t2) FMT. Println (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ")} / * {0 0} main.Teacher ,{ 0 0} ,{"" '\x00' '\x00'} {JackMa 10086 1} --------------- {pink 132 1} ------------------------- */Copy the code
1.2 Structure grammar sugar
Package main import "FMT" type teacher struct {name string age int8 sex byte} func SynSugar() Arr: [6] = int 10,20,30,40,50,60} {arr2: = & arr FMT. Println ((* arr2) [len (arr) - 1)) / / return 60 FMT. Println (arr2 [0]) / / / / return 10 slices of syntactic sugar Arr3 :=[4]int {100,200,300,400} arr4:=&arr3 ftt. Println((*arr4)[len(arr3)-1]) // return 400} func main() { Struct emp1:=new(teacher) fmt.Printf("emp1:%T,\n%v\n,%p\n",emp1,emp1,emp1) //*main. Teacher,&{0 0},0xc000004078 (*emp1).age=100 (*emp1).name="JackMa" (*emp1).sex=1 fmt.Println("--------------") fmt.Println(emp1) //&{JackMa 100 1} fmt.Println(*emp1) //{JackMa 100 1} fmt.Println("--------------") SynSugar() }Copy the code
1.3 Structs are value types
A deep copy is a copy of a value; a shallow copy is a copy of a pointer.
Package main import "FMT" type Dog struct {name string color string age int kind string} func main() {// Dog1 :=Dog{" black",2," I am black!" } fmt.Printf("d1:%T,%v,%p\n",dog1,dog1,&dog1) },0xc000042040 dog2:=dog1 fmt.Printf("dog2:%T,%v,%p\n",dog2,dog2,&dog2) dog2.name="JackMa" fmt.Println(dog2) //{JackMa Black 2 I am fortune! } FMT. Println (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ") / / dog2 boyfriend = "Steven" here inside structure without error / / 2 and realize the structure of the definition of a shallow copy, With new () function to realize dog4: = new (Dog) dog4. Name = "Tony Ma" dog4. Color = "white" dog4. Age = 100 dog4. Kind = "express" FMT. Printf (" dog4: %T,%v,%p\n",dog4,dog4,&dog4) // Return dog4: *main.Dog,&{Tony Ma white 100 cute}, 0xC000006030}Copy the code
1.4 Structure as a parameter return value
Structs can be returned as parameter values in two ways, value passing and parameter passing. (HERE I have a little confusion, go inside have reference parameter pass?) )
Package main import "FMT" type Flower struct {name,color string} func changeInfo(f Flower) {f.name=" FMT" F ="pink"} func changeInfo2(f *Flower) {f name=" flower1 "f color="white"} Func getFlower2() *Flower{temp:=Flower{" hibiscus ","ret"} Printf("%T,%v,%p\n",temp,temp,&temp) //main.Flower,{lotus ret}, 0xC000020500 f:=&temp return f} func main() { / / structure as a parameter: the usage of the f1 = Flower {" rose ", "red"} FMT) Printf (" % % % T, v, p/n ", f1, f1, & f1) changeInfo (f1) FMT. Printf (" % % % T, v, p/n ", f1, f1, & f1) / / the main Flower, rose red {}, 0 xc0000203e0 FMT. Println (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ") changeInfo2 (& f1) / / reference variable Printf("%T,%v,%p\n",f1,f1,&f1) //main.Flower,{morning Flower white}, 0xC0000203e0 F3: = getFlower2 (FMT). Println (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ") FMT) Printf (" % % % T, v, p/n ", f2, f2, & f2). / / the main Flower, {more meat Pink}, 0xC0000204e0 fmt.Printf("%T,%v,%p\n",f3,f3,&f3)Copy the code
The temp address in getFlower2() is not the same as the structure pointer address in RET. So there are references passed
1.5 Anonymous structures
An anonymous structure is a structure that does not need to be defined by type. When you create an anonymous structure, you also create an object. The anonymous structure consists of two parts: structure definition and key-value pair initialization.
Package main import (" FMT ""math") func main() {return math.pow (a,b)}(2,3) Struct {type,city string}{" 中 文 "," 中 文 "} struct.Println(addr) {struct {type,city string}{" 中 文 "," 中 文" Struct {name,color string age int8}{" black cat "," white ",10} FMT.Println(cat)}Copy the code
1.6 Anonymous fields for structs
Package main import "FMT" type User struct {string byte int8 float64} func main() {// instantiate the structure User1 :=User{" Steven ",'m',35,177.5} fmt.Println(user1) return {Steven 109 35 177.5} // If you want to print the name, age, height and sex at once Fmt.printf (" Name :%s\n",user1.string) // Return name: Steven FMt.printf (" Height :%.2f\n",user1.float64) // Height :177.50 FMT. Printf (" \ n "gender: % c, user1. Byte) / / gender: m FMT Printf (" age: % d \ n", user1, int8) / / age: 35}Copy the code
1.7 Structure nesting
Structure nesting is the use of one structure as an attribute (field) of another structure. This structure is called structure nesting. Structure nesting can simulate the following two relationships for object-oriented programming
- Aggregate relationship: One class as an attribute of another class.
- Inheritance: One class is a subclass of another. The relationship between subclasses and character classes.
Sample aggregation relationship
package main import "fmt" type Address struct { province,city string } type person struct { name string age int address P :=person{} p.name="Steven" p.age=19 // addr:=Address{} addr. Addr. city=" "p.adddress =&addr FMT. Println (" name: ", p.n ame, "age", p.age, ", "province, p.a ddress. The province," city ", p.a ddress. City) FMT. Println (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ") p.a ddress. City = "Lin zhi" Fmt.println (" name :",p.name,"age:",p.age,",p.age,"province:",p.address. city,"city:",p.address.city) // Modify whether the assignment object addr affects the data of the Person object Addr. City = "xigaze" FMT. Println (" name: ", p.n ame, "age", p.age, ", "province, p.a ddress. The province," city ", p.a ddress. City) FMT. Printf (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- \ n ") / / assignment method 2 p.a ddress = & Address {" qinghai ", "xining}" FMT.Println(" name :",p.name,"age:",p.age," type :",p.age :",p.age," type :", p.dress. type,"city:", p.dress. city)} City: 19 province: Tibet Lhasa -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- name: Steven age: 19 province: city: Tibet nyingchi name: Steven age: 19 Province: Xigaze ------------------ Name: Steven Age: 19 Province: Xining */Copy the code
1.8 Imitating inheritance relationships
StudentS carry on PersonS
package main import "fmt" type PersonS struct { name string age int sex string } type studentS struct { PersonS StudentNum int64} func main() {// PersonS{"JackMa",35," male "} FMT.Println(p1) FMT. Println (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - ") / / instantiate and initialize the Student / / write 1 s1: studentS of = {p1, 1008611} FMT. Println (s1) / / writing 2 S2 :=studentS{PersonS{"TonyMa",30," male "},123456} FMT.Println(s2) s3:=studentS{} s3 S3. Sex = "male"}Copy the code