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 structs (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:
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:
A method is like a function, but attached to a type (called as receiver). The official guide states “A method is a function with a special receiver argument”. The receiver appears in between the func keyword and the method name. The syntax of a method is:
type MyType string
t1 := MyType("sample")
t1.add(1,2)
Now lets bring pointers into the table. Go lang is pass by value, means fresh copies of the parameters are passed to each function/method call. To pass them by reference you can use pointers.
Syntax of function with pointer in argument/parameter list.
type MyType string
t1 := MyType("sample")
t1.add(2,3)
Note that we can still write t1.add() to execute the method with a pointer receiver(even-though ‘t1’ is not a pointer) and Go will interpret it as (&t1).add(). Similarly a method with value receiver can be called using pointer too, Go will interpret p.add() as as (*p).add() in that case (where ‘p’ is a pointer). This is applicable only for methods and not for functions.
Methods with pointer receiver are very useful to get a “Java” like behavior where the method is actually modifying on the value to which the receiver points and not on a copy of it.
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.
Above answer seems good to me. Along with that I would also like to add on it.
Function is something which is invoked from package. Whereas method is also a function but invoked from particular type values. And values is the return of functions.
For example:
rightNow := time.Now()
time --> package, Now() --> function because invoked by a package, rightNow is the value of type time.Time returned from Now() function.
year := rightNow.Year()
rightNow --> value, Year() --> function but since it is invoked by a value of some type(here time.Time), therefore it is called a Method. For easily identication Method has a reciever of that same type from which it is invoked.