3 ms·
They basically are functions with an additional argument. You can call them with the method syntax, but you can also call them like a regular function. To use
by throwaway34241 6y ago
They basically are functions with an additional argument. You can call them with the method syntax, but you can also call them like a regular function. To use your first example, you can also call it like this:
(*Foo).sayHello(f) // the same as f.sayHello()
It's mainly useful when you want to get a reference to the method itself:
f := (*Foo).sayHello // a function that takes one arg of type *Foo
For most purposes the two are the same, the main difference is with with how interfaces work in Go. Functions declared with the special syntax can be used to satisfy interfaces, which in Go means keeping some run-time type information around.