Imagine a golang executable as a house with only a front door and many different rooms inside it. You can go through any door you want once you are in the house, but to get inside you have to go through the front door first. That front door is the main() function.
Golang's entry point into an executable is through main(). If you want to run different logic paths for a single executable, you can use main() as a routing function to the other packages using command line arguments:
package main
import (
"os"
"otherpackage"
// Your child packages get imported here.
)
func main() {
// The first argument
// is always program name
// So os.Args[1] is the first dynamic argument
arg1 := os.Args[1]
// use arg1 to decide which packages to call
if arg1 == "option1" {
// option1 code executes here.
otherpackage.DoThis()
}
if arg1 == "option2" {
// option2 code executes here.
otherpackage.DoThat()
}
}
Then you can run your program with something like:
go run main.go option1
From golang documentation:
Program execution
A complete program is created by linking a single, unimported package called the main package with all the packages it imports, transitively. The main package must have package name main and declare a function main that takes no arguments and returns no value.