Coming from C, I naturally assumed that writing a struct function in Go would be as easy as creating a function pointer. Instead, struct methods can be specified with an additional type in any function declaration.
Say for example, we have the following struct:
type Point struct {
x int
y int
}
To write a Point method, simply add the Point type to the method declaration:
func (p Point) InvertPoint() {
temp := p.x
p.x = y
p.y = temp
}
While it does take some getting used to, I personally find go struct methods to be easier to work with than function pointers in C. When writing structs in C, you have to declare all of your function pointers (and therefore, how many struct methods you have) within the struct itself. In Go, a struct can have as many methods as needed regardless of what the struct says.