Go tips
1 min readAug 21, 2020
Function declaration syntax: things in parenthesis before the function name
Another explanation which is helpful can be found HERE.
A fantastic example from the above link can be found Goplayground
package mainimport (
"fmt"
"errors"
"math"
)type Triangle struct {
a, b, c float64
}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
}func main() {
t1 := Triangle{3, 4, 5}fmt.Println(t1.perimeter())
fmt.Println(t1.square())
}
iota
- The
iota
keyword represents successive integer constants 0, 1, 2,… - It resets to 0 whenever the word
const
appears in the source code, - and increments after each const specification