Thursday 15 March 2012

go - How do I dynamically iterate through a package? -



go - How do I dynamically iterate through a package? -

this first week go, please forgive ignorance ;). come in peace python. building simple calculator add-on , subtraction currently.

my addition.go file looks this:

package calculator type add together struct{} func (h add) first(x int) int { x += 5 homecoming x } func (h add) second(x int) int { x += 10 homecoming x }

the layout of subtraction.go file looks similar addition.go , future features multiplication.go , division.go similar. run these 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 calc := &calculator.add{} = i.first(x) fmt.println(x) fmt.println(i.first(x)) fmt.println(i.second(x)) fmt.println("next method...") b := &calculator.sub{} = b fmt.println(x) fmt.println(i.first(x)) fmt.println(i.second(x)) // iterate through rest of calculator methods }

this seems verbose, add together more , more features multiplication, etc. there way find methods of calculator & iterate on of them? i've looked through reflect documentation there doesn't seem way this. order in run doesn't matter....eg subtraction can run before addition.

go not provide feature want. there no mechanism introspect contents of packages. reason compiler keeps functions , variable in executable referenced somewhere. if never explicitly utilize function, won't in executable. iterating on perchance incomplete set of symbols pointless , hence not implemented.

you create array containing objects of types want operate on , iterate on if solves problem.

go

No comments:

Post a Comment