Go Function
# Function Closure
package main
import "fmt"
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
f2, f1 := 0, 1
// return a function
return func() int {
f := f2
f2, f1 = f1, f+f1
return f
}
}
func main() {
f := fibonacci() // assign
for i := 0; i < 10; i++ {
fmt.Println(f())
}
}
# Methods
A method is just a function with a receiver argument.
func (v Vertex) Abs() float64 {
return math.Sqrt(v.X*v.X + v.Y*v.Y)
}
// (v Vertex) is a receiver
You can only declare a method with a receiver whose type is defined in the same package as the method. You cannot declare a method with a receiver whose type is defined in another package (which includes the built-in types such asĀ int).
You can declare methods with pointer receivers.
func (v *Vertex) Scale(f float64) {
v.X = v.X * f
v.Y = v.Y * f
}
Go interprets the statementĀ v.Scale(5)Ā asĀ (&v).Scale(5)Ā since theĀ ScaleĀ method has a pointer receiver.