在同一个包中将实现分开放在自己的文件中。

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

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())
        },
    }

huangapple
  • 本文由 发表于 2015年3月24日 04:41:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/29219958.html
匿名

发表评论

匿名网友

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

确定