Golang Functions and Methods Syntax
These are “notes-to-self” reminder to help me learn Golang. Coming from Python, I find the syntax being unfamiliar and have to look it up everytime I want to write a function or a method. Until writing function becomes second nature, this article will act as a reminder to myself as well as to those who are in need.
For a function, the syntax is:
func FunctionName(parameters) return_type {
// body
}
For example:
func AddTip(amount float64) float64 {
// ...
}
For a method, the syntax is:
func (receiver) FunctionName(parameters) return_type {
// ...
}
For example, given a struct User:
func (u User) IsAdmin() bool {
return u.uid == 0
}
// Usage. Given user is of type User
if (user.IsAdmin()) {
// ...
}