This is my first week with Go, so please forgive the ignorance ;). I come in peace from Python. I am building a simple calculator with Addition and Subtraction currently.
My addition.go
file looks like this:
package calculator
type Add struct{}
func (h Add) First(x int) int {
x += 5
return x
}
func (h Add) Second(x int) int {
x += 10
return x
}
The layout of my subtraction.go
file looks very similar to addition.go
and future features like multiplication.go
and division.go
will look similar. To run these I have:
package main
import (
"fmt"
"github.com/mytester/calculator"
)
type Calc interface {
First(x int) int
Second(x int) int
}
func main() {
x := 10
var i Calc
a := &calculator.Add{}
i = a
i.First(x)
fmt.Println(x)
fmt.Println(i.First(x))
fmt.Println(i.Second(x))
fmt.Println("Next method...")
b := &calculator.Sub{}
i = b
fmt.Println(x)
fmt.Println(i.First(x))
fmt.Println(i.Second(x))
// iterate through the rest of my calculator methods
}
This seems very verbose, especially as I add more and more features like multiplication, etc. Is there a way to find all the methods of my calculator & then iterate over all of them? I've looked through the reflect
documentation but there doesn't seem to be a way to get this. The order in which they are run doesn't matter....eg subtraction can run before addition.