This is a creation in Article, where the information may have evolved or changed.
In C, we declare a function in a struct, and go cannot declare a function in a struct. Instead, it uses a different form of existence, called method in Go.
The concept of method
method is subordinate to a given type, the syntax and function of the declaration syntax is almost the same, but again after the Func added a recevier (that is, the subject of method compliance)
Syntax format for method
func (r ReceiverType) funcName(parameters) (results)
The image is said, is the Receivertype type of all fields, method funcName can be used, you can think FuncName belongs to Receivertype.
Simple application of method
Package Mainimport ("FMT" "Math") type Rectangle struct {width, height float64}type Circle struct {radius float64}func (r Rectangle) area () float64 {return R. Width* R. Height}func (c Circle) area () Float64 {return C. Radius* C. Radius* Math. Pi}func Main () {R1: = rectangle{ A,2}R2: = rectangle{9,4} C1: = circle{Ten} C2: = circle{ -} FMT. Println("Area of R1 is:",R1. Area()) FMT. Println("Area of R2 is:",R2. Area()) FMT. Println("Area of C1 is:", C1. Area()) FMT. Println("Area of C2 is:", C2. Area())}
Output:
ofis: 24ofis: 36ofis: 314.1592653589793ofis: 1963.4954084936207
The relationship between Rect and Circle and the area () method attached to them is as follows:
- Method is passed. To access, just like accessing a struct inside a field.
- In the method, you can access the fields of the recipient, such as R1.area (), to access the width and height of the R1.
- Although the name of the method is the same, but different receiver is not the same, then the method is not the same. This is important.
- Also, the method can not only function on a struct, but also define any type of custom, built-in type, etc.
- Receiver in method can be either a value pass or a pointer. Pointer, you can directly modify the contents of receiver
Pointer usage of method
Format:
func (r *ReceiverType) funcName(parameters) (results)
See Example:
package mainimport"fmt"typestruct { string age int32}funcstringint32) { p.name = name p.age = age}func main() { new(person) p.setPerson("uu_dou", 15) fmt.Println(p.name) fmt.Println(p.age)}
Output:
uu_dou15
If receiver in the previous example is not a pointer, the output is:
//name什么都没有0//age 默认为0
In contrast to the previous example, it is not difficult to see whether receiver in the method is a value pass or a pointer. Symbolic access, because Go knows receiver is not a pointer.
Method inheritance and rewriting
Inheritance: If an anonymous segment in a struct implements a method, the struct that contains the anonymous segment can also invoke the method.
Rewrite: If an anonymous segment in a struct implements a method, the struct containing the anonymous segment can override the anonymous field.
Summarize
The knowledge of method is an important basis for the object-oriented go implementation, it is important to master it. It is not the same as Java, C + +, there is no private, public and other keywords, still by the case of the implementation (uppercase beginning of the common, lowercase beginning of the private), methods also apply this principle.