[Question/Requesting Help] modular-golang functionality

Hey,

I am pretty new to golang but not coding. I was trying to create a modular golang program (main.go) that will read in all the other go files in a template format like this:
programA.go:
programA_funcA{
do this}
programA_funcB{
do this}

The goal is to have be able to drop any file named programX.go file into the project folder following the same function naming scheme above and have my main.go read in all the programX.go files and run through the template commands

So if I had programA.go and programB.go, it would be something like

foreach programX.go{
$program.funcA
$program.funcB
}

output:
programA.funcA
programA.funcB
programB.funcA
programB.funcB

Is something like this possible in go? All advice would be helpful!

It’s possible, but I wouldn’t recommend it for lack of clarity. Nonetheless, something like this works:

main.go

var funcs []func()

func main() {
  for _, fn : = range funcs {
    fn()
  }
}

func_a.go

func init() {
    funcs = append(funcs, funcA)
}

func funcA() {
    fmt.Println("Hello from funcA")
}

(You can have one (or several) func init() in every file)

2 Likes

Thanks for the quick response! I think that is exactly what I was looking for.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.