Method values and method expressions in GO are used in detail

  • 2020-10-07 18:43:56
  • OfStack

Go, also known as Golang, is characterized by efficiency, performance, security and robustness.

The Go language supports concurrency natively from the bottom up, and it doesn't require a third party library, developer programming skills, or development experience to do so easily. This article focuses on introducing go method values and method expressions.

The explanation of this section in the manual is not very detailed and clear. After several examples, I have summarized the usage of the next section.

Method expressions: Simply, method objects are assigned to variables.

Here are two ways to use it:

1) Method value: implicitly call, struct instance to obtain the method object

2) Method expression: display call, struct type gets method object, need to pass struct instance object as parameter.

Here's an example:


package main
import (
 "fmt"
)
type Student struct {
 id int
 name string
}
func (s *Student) SkillPointer() {
 fmt.Printf(" Pointer function :%p, %v\n", s, s)
}
func (s Student) SkillValue() {
 fmt.Printf(" Value type function : %p, %v\n", &s, s)
}
func main() {
 s := Student{1, " Steve "} //  Instantiate the structure 
 // General usage 
 s.SkillPointer()
 fmt.Println(".............................\n")
 // Method expression 
 sFunc1 := (*Student).SkillPointer // Note the direct use here   Pointer type structure name . methods 
 sFunc1(&s)      // Show the receiver *Student Transfer the past 
 sFunc2 := Student.SkillValue // Note the direct use here   Structural name . methods 
 sFunc2(s)     // Show the receiver Student Transfer the past 
 fmt.Println(".............................\n")
 // Methods the value 
 sFunc3 := s.SkillPointer // This is the method value, and when you call the function, you don't have to pass the receiver, hiding the receiver 
 sFunc3()     //  Isn't it   It's kind of like an anonymous function call, declaring a variable sFunc3  Is the method name of the structure, then the variable () The call. A little more than this 1 Lift the feeling hair? 
 sFunc4 := s.SkillValue // Since no pointer method is called, this completes the content copy operation. What's the difference 
 sFunc4()
 fmt.Println(".............................\n")
}

Output:

[

Pointer function :0xc00007c060, & {1 Mr. Jobs}
.............................
Pointer type function :0xc00007c060, & {1 Mr. Jobs}
Value type function: 0 xc00007c0e0, Steve {1}
.............................
Pointer type function :0xc00007c060, & {1 Mr. Jobs}
Value type function: 0xc00007c140, {1 Steve Jobs}
.............................

]

The method is worth copying. What's the difference? Let's see:


package main
import (
 "fmt"
)
type Student struct {
 id int
 name string
}
func (s *Student) SkillPointer() {
 fmt.Printf(" Pointer function :%p, %v\n", s, s)
}
func (s Student) SkillValue() {
 fmt.Printf(" Value type function : %p, %v\n", &s, s)
}
func main() {
 u := Student{1, " Steve "}
 mValue := u.SkillValue //  Because it is not a pointer type, it is not affected by the following changes. 
 u.id, u.name = 2, " Dove the wisdom "
 u.SkillValue()
 mValue() //  The value has not changed and is not affected 
}

Output:

[

Value type function: 0xc0000044c0, {2 Jiumaozhi}
Value type function: 0xc000004520, {1 Steve Jobs}
To update them all, change to mValue := u.SkillPointer pointer function.

]

No matter what industry you are in, it is enough to do two things well, one is your major and the other is your personality. Your major determines your existence, while your personality determines your contacts. The rest is to persist and win more trust with kindness and sincerity.

conclusion


Related articles: