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

我不太确定如何在围棋中实现同样的事情,因为看起来每个围棋项目应该只有一个主可执行文件.

我想从cronjob调用项目中的函数,但我不太确定如何添加它.在我的主函数中使用标志是我能想到的唯一方法.但是,如果我的脚本本身接受以下附加标志,则看起来会非常混乱:

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

有什么优雅的方法可以做到这一点吗?

推荐答案

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.

另一种 Select 是使用像spf13/cobra这样的CLI接口库,它允许您定义几个commands(相同的exe,不同的选项集).

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.

在示例"hugo server --port=1313"中,"server"是命令

A Command具有以下 struct :

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.
}

Go相关问答推荐

使用ciph.AEAD.Seal()查看内存使用情况

如何使用Gorilla WebSockets实现Http.Hijacker&;alexedwards/scs/v2

Golang使用Run()执行的命令没有返回

按位移计算结果中的差异

我应该先解锁然后再广播吗?

使用goroutines在Golang中验证 struct

使用golang sqlc中的引用参数

Global Thread-local Storage 在 Go 中的可行性和最佳实践

如何使用 go 包设置标头键和值:shurcooL/graphql 或 hasura/go-graphql-client?

如何使用管理员权限启动 powershell 进程并重定向标准输入 (os.exec)

上传图片失败,出现错误dial tcp: lookup api.cloudinary.com: no such host

整理时转换值

Wire google Inject with multi return from provider 函数

CORS grpc 网关 GoLang

Golang并发写入多个文件

使用 AppID 在 Windows 中启动应用程序并获取 pid

转到文本/模板模板:如何根据模板本身的值数组判断值?

无限期运行 Go routine (完成后重新启动)

将未知长度切片的值分配给Go中的 struct ?

Golang 泛型同时具有接口和实现