SoFunction
Updated on 2025-03-05

GO language structure object-oriented operation example

Anonymous field initialization

package main
import "fmt"
type Person struct {
	name string //name	sex  byte   //gender	age  int    //age}
type Student struct {
	Person //Only type, no name, anonymous field, inherits Person's members	id     int
	addr   string
}   
func main() {
	//Sequential initialization	var s1 Student = Student{Person{"mike", 'm', 18}, 1, "bj"}
	("s1 = ", s1)
	//Automatic derivation type	s2 := Student{Person{"mike", 'm', 18}, 1, "bj"}
	//("s2 = ", s2)
	//%+v, display more detailed	("s2 = %+v\n", s2)
	//Specify member initialization, the commonly used automatic assignment of 0 without initialization is 0	s3 := Student{id: 1}
	("s3 = %+v\n", s3)
	s4 := Student{Person: Person{name: "mike"}, id: 1}
	("s4 = %+v\n", s4)
	//s5 := Student{"mike", 'm', 18, 1, "bj"} //err
}

Member operations

package main
import "fmt"
type Person struct {
	name string //name	sex  byte   //Gender, Character Type	age  int    //age}
type Student struct {
	Person //Only type, no name, anonymous field, inherits Person's members	id     int
	addr   string
}
func main() {
	s1 := Student{Person{"mike", 'm', 18}, 1, "bj"}
	 = "yoyo"
	 = 'f'
	 = 22
	 = 666
	 = "sz"
	 = Person{"go", 'm', 18}
	(, , , , )
}

Same name

package main
import "fmt"
type Person struct {
	name string //name	sex  byte   //Gender, Character Type	age  int    //age}
type Student struct {
	Person //Only type, no name, anonymous field, inherits Person's members	id     int
	addr   string
	name   string //The same name as Person}
func main() {
	//Declare (define a variable)	var s Student
	//Default rules (tangled principle), if this member can be found in this scope, then this member will be operated	// If not found, find the inherited field	 = "mike" //Is it the name of Student or Person?, the conclusion is Student's name	 = 'm'
	 = 18
	 = "bj"
	//Explanatory call	 = "yoyo" //Person's name	("s = %+v\n", s)
}

Non-structure anonymous segment

package main
import "fmt"
type mystr string //Customize the type, change the name of a typetype Person struct {
	name string //name	sex  byte   //Gender, Character Type	age  int    //age}
type Student struct {
	Person //The structure anonymous field	int    //Basic type anonymous segment	mystr
}
func main() {
	s := Student{Person{"mike", 'm', 18}, 666, "hehehe"}
	("s = %+v\n", s) //s = {Person:{name:mike sex:109 age:18} int:666 mystr:hehehe}
	 = Person{"go", 'm', 22}
	(, , , , )  // go 22 109 666 hehehe
	(, , )  // {go 109 22} 666 hehehe
}

Anonymous field of structure pointer type

package main
import "fmt"
type Person struct {
	name string //name	sex  byte   //Gender, Character Type	age  int    //age}
type Student struct {
	*Person //Pointer type	id      int
	addr    string
}
func main() {
	s1 := Student{&Person{"mike", 'm', 18}, 666, "bj"}
	(, , , , )
	//Define the variable first	var s2 Student
	 = new(Person) //Allocate space	 = "yoyo"
	 = 'm'
	 = 18
	 = 222
	 = "sz"
	(, , , , )
}

The difference between process-oriented and object functions

package main
import "fmt"
//Realize the addition of 2 numbers//Process-orientedfunc Add01(a, b int) int {
	return a + b
}
//Object-oriented, method: bind a function to a certain typetype long int
//tmp is called the receiver, the receiver is a parameter passedfunc (tmp long) Add02(other long) long {
	return tmp + other
}
func main() {
	//var result int
	//result = Add01(1, 1) //Ordinary function call method	//("result = ", result)
	//Define a variable	var a long = 2
	//Call method format: variable name. function (required parameters)	r := a.Add02(3)
	("r = ", r)
	//Object-oriented just changes to a form of expression}

Add method to structure type

package main
import "fmt"
type Person struct {
	name string //name	sex  byte   //Gender, Character Type	age  int    //age}
//The function with the receiver is called a methodfunc (tmp Person) PrintInfo() {
	("tmp = ", tmp)
}
//A function to assign values ​​to membersfunc (p *Person) SetInfo(n string, s byte, a int) {
	 = n
	 = s
	 = a
}
func main() {
	//Definition is initialized simultaneously	p := Person{"mike", 'm', 18}
	()  //tmp =  {mike 109 18}
	//Define a structure variable	var p2 Person
	(&p2).SetInfo("yoyo", 'f', 22)
	() //tmp =  {yoyo 102 22}
}

Pointer variable method set

package main
import "fmt"
type Person struct {
	name string //name	sex  byte   //Gender, Character Type	age  int    //age}
func (p Person) SetInfoValue() {
	("SetInfoValue")
}
func (p *Person) SetInfoPointer() {
	("SetInfoPointer")
}
func main() {
	//Structure variable is a pointer variable. What methods can it call? These methods are a collection, referred to as method sets for abbreviation	p := &Person{"mike", 'm', 18}
	() //func (p *Person) SetInfoPointer()
	(*p).SetInfoPointer() //Change (*p) to convert layer p and then call it, which is equivalent to the above	//Internally made conversion, first convert the pointer p, and then call it	//(*p).SetInfoValue()  //SetInfoValue
	//()  // SetInfoValue
}

Normal variable method set

package main
import "fmt"
type Person struct {
	name string //name	sex  byte   //Gender, Character Type	age  int    //age}
func (p Person) SetInfoValue() {
	("SetInfoValue")
}
func (p *Person) SetInfoPointer() {
	("SetInfoPointer")
}
func main() {
	p := Person{"mike", 'm', 18}
	() //func (p *Person) SetInfoPointer()
	//Internally, first convert p, to &p and then call, (&p).SetInfoPointer()	()  //Call directly, no need to transfer internally}

Inheritance of methods

package main
import "fmt"
type Person struct {
	name string //name	sex  byte   //Gender, Character Type	age  int    //age}
//Person type, implements a methodfunc (tmp *Person) PrintInfo() {
	("name=%s, sex=%c, age=%d\n", , , )
}
//There is a student who inherits Person field, and both members and methods have been inherited.type Student struct {
	Person //Anonymous field	id     int
	addr   string
}
func main() {
	s := Student{Person{"mike", 'm', 18}, 666, "bj"}
	()
}

Rewriting method

package main
import "fmt"
type Person struct {
	name string //name	sex  byte   //Gender, Character Type	age  int    //age}
//Person type, implements a methodfunc (tmp *Person) PrintInfo() {
	("name=%s, sex=%c, age=%d\n", , , )
}
//There is a student who inherits Person field, and both members and methods have been inherited.type Student struct {
	Person //Anonymous field	id     int
	addr   string
}
//Student also implements a method, which is the same name as the Person method. This method is called rewritingfunc (tmp *Student) PrintInfo() {
	("Student: tmp = ", tmp)
}
func main() {
	s := Student{Person{"mike", 'm', 18}, 666, "bj"}
	//The principle of proximity: first find the method of this scope, and then use the inherited method.	() //Is Person or Student calling? The conclusion is Student	//Explanatory call to inherited methods	()
}

Method value

package main
import "fmt"
type Person struct {
	name string //name	sex  byte   //Gender, Character Type	age  int    //age}
func (p Person) SetInfoValue() {
	("SetInfoValue: %p, %v\n", &p, p)
}
func (p *Person) SetInfoPointer() {
	("SetInfoPointer: %p, %v\n", p, p)
}
func main() {
	p := Person{"mike", 'm', 18}
	("main: %p, %v\n", &p, p)
	//() //Traditional calling method	//Save method entry address	pFunc :=  //This is the method value. When calling the function, there is no need to pass the receiver anymore, and the receiver is hidden	pFunc()                   //Equivalent to ()	vFunc := 
	vFunc() //Equivalent to ()}

Method expression

package main
import "fmt"
type Person struct {
	name string //name	sex  byte   //Gender, Character Type	age  int    //age}
func (p Person) SetInfoValue() {
	("SetInfoValue: %p, %v\n", &p, p)
}
func (p *Person) SetInfoPointer() {
	("SetInfoPointer: %p, %v\n", p, p)
}
func main() {
	p := Person{"mike", 'm', 18}
	("main: %p, %v\n", &p, p)
	//Method value f := //Hide the receiver	//Method expression	f := (*Person).SetInfoPointer
	f(&p) //Explanatory pass the receiver ====> ()	f2 := (Person).SetInfoValue
	f2(p) //Explanatory pass the receiver ====> ()}

The above is the detailed content of the GO language structure object-oriented operation example. For more information about GO language structure object-oriented, please pay attention to my other related articles!