在Go语言中有多个主函数

huangapple go评论105阅读模式
英文:

Having multiple main functions on Go

问题

作为Python和Django开发者,我可以使用脚本独立地运行项目中的任何代码。

我不太确定如何在Go中实现相同的功能,因为每个Go项目似乎只能有一个主可执行文件。

我想从cronjob中调用项目中的一个函数,但我不太确定如何添加。我唯一能想到的方法是在我的主函数中使用标志。但如果我的脚本自己接受额外的标志,那么看起来会很混乱,就像下面这样:

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

有没有更优雅的方法来实现这个?

英文:

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 &lt;MY-SCRIPT&gt; --my-script-flag-one &lt;FLAG-ONE&gt; --my-script-flag-two &lt;FLAG-TWO&gt;

Is there any elegant way of doing this?

答案1

得分: 25

我在《如何合理地布局Go项目》这篇文章中引用了《在Go中构建应用程序》,该文章以项目**perkeep**作为示例。该项目包含多个 cmd包,每个包都有自己的选项集。

另一种选择是使用CLI界面库,比如spf13/cobra,它允许你定义多个命令(同一个可执行文件,不同的选项集)。

> Command是应用程序的中心点。
应用程序支持的每个交互都将包含在一个Command中。
一个命令可以有子命令,并可选择运行一个操作。

> 在示例中的"hugo server --port=1313"中,'server'是命令。

> Command具有以下结构:

type Command struct {
    Use string // 一行的用法消息。
    Short string // 在'help'输出中显示的简短描述。
    Long string // 在'help <this-command>'输出中显示的长消息。
    Run func(cmd *Command, args []string) // 运行命令。
}
英文:

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 &#39;help&#39; output.
    Long string // The long message shown in the &#39;help &lt;this-command&gt;&#39; output.
    Run func(cmd *Command, args []string) // Run runs the command.
}

答案2

得分: 1

在Go语言中,你可以拥有多个main函数,但是包的声明必须是main。这样,你可以在main包内拥有多个Go脚本/文件,每个文件都有一个main函数。要运行一个文件,你可以直接执行该文件,例如go run file.go,这将只执行你调用的文件的main函数。

英文:

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.

huangapple
  • 本文由 发表于 2014年11月26日 10:00:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/27140130.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定