Can any one explain this go program which uses a method expression

Thinker :

I was going through some go tutorials but I'm not able to understand what method expressions are in go. Can anyone explain this code to me and why/when I should use it?

 // Method call with "method expression" syntax
 func main() {
    dog := Dog{}
    b := (*Dog).Bark // method expression 
    b(&dog, 5)
 }
 type Dog struct {}

 // Methods have a receiver, and can also have a pointer
 func (d *Dog) Bark(n int) {
   for i := 0; i < n; i++ {
      fmt.Println("Bark");
   }
 }
Marc :

A method expression is a function that can be called like a regular function, except that you also pass the object to act on as the first argument. This is because it needs to know which object to use.

Normally, you would just use the following:

d := &Dog{}
d.Bark(5)

But using a method expression, you can "save" the function, allowing you to pass it to something else. For example, you could choose to use (*Dog).Bark or (*Dog).Sit as an action, and call it from a helper. eg:

func main() {
    var b func(*Dog, int)
    if (shouldBark) {
        b = (*Dog).Bark
    } else {
        b = (*Dog).Sit
    }
    d := Dog{}
    DoAction(b, &d, 3)
}

func DoAction(f func(*Dog, int), d *Dog, n int) {
    f(d, n)
}

The specific syntax (*Dog).Bark means that you are deriving a function for a method with pointer receiver.

Honestly, these are not very frequently used. I would recommend you get a good grasp of the language first (eg: take the entire Go tour), then explore less used functionality at a later date.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

can any one explain in detail about the following program of pointers in c?

Explain the output of the program in detail which uses 3D arrays?

Can any one explain in which case we use PowerMockito.when() and PowerMockito.doReturn()

Can any one please explain which indexes to refresh in magento after mentioned changes

Can any one explain Screenshot in Selenium?

Friends Can any one explain this code

Can any one explain the logic behind this code

${$var.'s_array'} can any one explain this?

Can any one explain to me what is "to poll the DOM" in Selenium?

Can any one explain me this syntax of predefined GenericModel class?

can any one explain meaning of mixed and buffer data type in mongoose?

can any one please explain me react setState new to me

Can any one explain this strange behavior about Array sort?

Can any one explain `${}` notation I cannot search that on google

Can any one explain the use of '&' in this piece of ruby code?

How can I bind expression which uses itself?

Is there any method to know which enviroment variables is trying to read a linux program?

Can someone explain this Kotlin expression?

Can someone explain this C++ code to me which is part of LinkedList program?

Running Go Program which uses OpenVINO & OpenCV - /usr/bin/ld: cannot find -lXXX

can any ask explain me this regular expression "pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}"?

How a program can know which method use (polymorphism)

Can you explain Go Interfaces?

Build an expression tree that can handle any type/class with a "TryParse()" method

is there any tactic in Coq that can transform a bool expression to a Prop one?

error: expected primary-expression before ‘>’: templated function that try to uses a template method of the class for which is templated

can any one help me to solve this python program?

Class declaration through which one can interact with any type of data

How to take any number of arguments from the user and pass them to a method which uses varargs( variable argument )?

TOP Ranking

HotTag

Archive