英文:
Separate Implementation in own files in same package
问题
我正在尝试使用Go语言进行编程。所以我发明了一个小项目,并希望构建一个带有不同命令的小型控制台应用程序。
我找到了codegangsta/cli并尝试了示例代码。
package main
import (
    "os"
    "github.com/codegangsta/cli"
)
func main() {
    app := cli.NewApp()
    app.Commands = []cli.Command{
        {
            Name:   "add",
            Usage:  "add a task to the list",
            Action: func(c *cli.Context) {
                println("added task:", c.Args().First())
            },
        },
        {
            Name:   "complete",
            Usage:  "complete a task on the list",
            Action: func(c *cli.Context) {
                println("completed task:", c.Args().First())
            },
        },
    }
    app.Run(os.Args)
}
现在我想将命令放在Commands数组中的单独文件中,并且只引用它们。
我该如何实现这一点?
谢谢您的建议。
英文:
I am try to get hands dirty with golang. So I invented a small project and want do build a small console app with different commands.
I found codegangsta/cli and tried the example.
    package main
import (
    "os"
    "github.com/codegangsta/cli"
)
func main() {
    app := cli.NewApp()
    app.Commands = []cli.Command{
        {
            Name:      "add",
            Usage:     "add a task to the list",
            Action: func(c *cli.Context) {
                println("added task: ", c.Args().First())
            },
        },
        {
            Name:      "complete",
            Usage:     "complete a task on the list",
            Action: func(c *cli.Context) {
                println("completed task: ", c.Args().First())
            },
        },
    }
    app.Run(os.Args)
}
Now I want to put the commands in Commands-array in separate files and only reference them.
How can I achieve this?
Thanks for some advice.
答案1
得分: 3
以下是将文件拆分的一种方法:
在 main.go 文件中:
package main
import (
    "os"
    "github.com/codegangsta/cli"
)
func main() {
    app := cli.NewApp()
    app.Commands = []cli.Command{
        addCommand,
        completeCommand,
    }
    app.Run(os.Args)
}
在 commands.go 文件中编写:
package main
import (
    "github.com/codegangsta/cli"
)
var addCommand = cli.Command{
    Name:  "add",
    Usage: "add a task to the list",
    Action: func(c *cli.Context) {
        println("added task: ", c.Args().First())
    },
}
var completeCommand = cli.Command{
    Name:  "complete",
    Usage: "complete a task on the list",
    Action: func(c *cli.Context) {
        println("completed task: ", c.Args().First())
    },
}
希望对你有帮助!
英文:
Here's one way of several ways to split the file up:
In file main.go:
package main
import (
    "os"
    "github.com/codegangsta/cli"
)
func main() {
    app := cli.NewApp()
    app.Commands = []cli.Command{
        addCommand,
        completeCommand,
    }
    app.Run(os.Args)
} 
In file comamnds.go write:
package main
import (
    "github.com/codegangsta/cli"
)
var addCommand = cli.Command{
        Name:      "add",
        Usage:     "add a task to the list",
        Action: func(c *cli.Context) {
            println("added task: ", c.Args().First())
        },
    }
 var completeCommand cli.Command{
        Name:      "complete",
        Usage:     "complete a task on the list",
        Action: func(c *cli.Context) {
            println("completed task: ", c.Args().First())
        },
    }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论