I am trying to get started with Go and the documentation is very good. What I did not find in the documentation is the difference between functions and methods.
As far as I understand at the moment: functions are "global", which means I do not have to import a package to use functions, they are always there. Methods are bound to packages. Is this correct?
Note: A frequently asked question is “what is the difference between a function and a method”. A method is a function that has a defined receiver, in OOP terms, a method is a function on an instance of an object. Go does not have classes. However, you can define methods on struct types.
Method and a function are the same, with different terms. A method is a procedure or function in object-oriented programming. A function is a group of reusable code which can be called anywhere in your program. This eliminates the need for writing the same code again and again.
Go language support methods. Go methods are similar to Go function with one difference, i.e, the method contains a receiver argument in it. With the help of the receiver argument, the method can access the properties of the receiver. Here, the receiver can be of struct type or non-struct type.
Go does not have classes. However, you can define methods on types. A method is a function with a special receiver argument. The receiver appears in its own argument list between the func keyword and the method name.
As far as I understand at the moment: functions are "global", which means I do not have to import a package to use functions, they are always there. Methods are bound to packages. Is this correct?
No, that's not correct. There are just a couple of functions from the builtin package which are always available. Everything else needs to be imported.
The term "method" came up with object-oriented programming. In an OOP language (like C++ for example) you can define a "class" which encapsulates data and functions which belong together. Those functions inside a class are called "methods" and you need an instance of that class to call such a method.
In Go, the terminology is basically the same, although Go isn't an OOP language in the classical meaning. In Go, a function which takes a receiver is usually called a method (probably just because people are still used to the terminology of OOP).
So, for example:
func MyFunction(a, b int) int { return a + b } // Usage: // MyFunction(1, 2)
but
type MyInteger int func (a MyInteger) MyMethod(b int) int { return a + b } // Usage: // var x MyInteger = 1 // x.MyMethod(2)
Tux's answer is great, but I want to augment it with the usage of Go's methods with struct
s (because this is where I used it often). So let's assume you want to build something to calculate various methods on triangles. You start with a struct
:
type Triangle struct { a, b, c float64 }
and then you would like to add some functions to calculate the perimeter and square:
func valid(t *Triangle) error { if t.a + t.b > t.c && t.a + t.c > t.b && t.b + t.c > t.a { return nil } return errors.New("Triangle is not valid") } func perimeter(t *Triangle) (float64, error) { err := valid(t) if err != nil { return -1, err } return t.a + t.b + t.c, nil } func square(t *Triangle) (float64, error) { p, err := perimeter(t) if err != nil { return -1, err } p /= 2 s := p * (p - t.a) * (p - t.b) * (p - t.c) return math.Sqrt(s), nil }
And now you got your working program Go Playground. In this case your function takes a parameter (pointer to a triangle) and does something. In OOP word people might have created a class and then added methods. We can see our struct as kind of class with fields and now we add methods:
func (t *Triangle) valid() error { if t.a + t.b > t.c && t.a + t.c > t.b && t.b + t.c > t.a { return nil } return errors.New("Triangle is not valid") } func (t *Triangle) perimeter() (float64, error) { err := t.valid() if err != nil { return -1, err } return t.a + t.b + t.c, nil } func (t *Triangle) square() (float64, error) { p, err := t.perimeter() if err != nil { return -1, err } p /= 2 s := p * (p - t.a) * (p - t.b) * (p - t.c) return math.Sqrt(s), nil }
and we have a fully working example.
Notice that it looks really like a method for objects.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With