27

As a Python and Django developer, I can run any piece of code in my project using a script independently.

I am not too sure how to achieve the same thing in Go, as it looks like each Go project should only have one main executable file.

I'd like to call a function in my project from cronjob, but I'm not too sure how to add that. Using flags in my main function is the only way I can think of doing this. But it will look pretty confusing if my script accepts additional flags by itself like the following:

go run server.go --debug --another-flag --script-name <MY-SCRIPT> --my-script-flag-one <FLAG-ONE> --my-script-flag-two <FLAG-TWO>

Is there any elegant way of doing this?

mohi666
  • 6,842
  • 9
  • 45
  • 51
  • You may find this question useful: [What is a sensible way to layout a Go project](http://stackoverflow.com/q/14867452/142162) –  Nov 26 '14 at 02:02
  • Make the piece of code a package and a command. For example, [Can I have a library and binary with the same name?](http://stackoverflow.com/questions/14284375/can-i-have-a-library-and-binary-with-the-same-name/14284575) – peterSO Nov 26 '14 at 06:13
  • some of the confusion is thinking of go as a scripting language. `go run` should definitely not be used for things other than playing around with a small test, e.g. something you'd otherwise put on `play.golang.org` – Dustin Nov 26 '14 at 23:30

2 Answers2

26

I reference in "What is a sensible way to layout a Go project" the article "Structuring Applications in Go", which shows as an example the project perkeep.
That project includes several cmd packages, each with their own set of options.

The other option would be to use a CLI interface library like spf13/cobra, which allows you to define several commands (same exe, separate sets of options).

Command is the central point of the application.
Each interaction that the application supports will be contained in a Command.
A command can have children commands and optionally run an action.

In the example "hugo server --port=1313", 'server' is the command

A Command has the following structure:

type Command struct {
    Use string // The one-line usage message.
    Short string // The short description shown in the 'help' output.
    Long string // The long message shown in the 'help <this-command>' output.
    Run func(cmd *Command, args []string) // Run runs the command.
}
Danny Kopping
  • 4,862
  • 2
  • 29
  • 38
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
1

You can have multiple main function in go but the package should be declared main. This way you can have multiple go scripts/file inside package main where each file will have main function. To run a file, you can directly execute the file like go run file.go which will only execute the main function of file you called.

Mayank Pant
  • 145
  • 1
  • 8
  • 3
    the GO compiler will complain if the new main() function (also package: main) is in a file which is in the same folder as another file of package main with a main() function. Multiple main() functions can co-exist if the file is in a separate folder (still of package main). So a new folder is needed per main() function – Paul P M Dec 12 '22 at 02:17
  • 2
    This is so annoying: "So a new folder is needed per main() function" – JohnAllen Dec 27 '22 at 12:12
  • No, there will be a package main. In that package you can create multiple files where each file can have there own main function. – Mayank Pant Jan 02 '23 at 09:39