Go, Golang : Program, Package Structure for Command Line Interface

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

Go, Golang : Program, Package Structure for Command Line Interface

问题

这是一个新手问题...假设我有一个名为"myprogram"的Go代码目录(在这里初始化为根目录),并且我在几个子目录中编写了一些分包。

然后我有:

repository,根目录

myprogram
----------- package1
----------- package2
----------- package3

然后在myprogram目录中,我将编写具有主要程序包的代码,并且主程序将调用在子目录中定义的所有包,如下所示:

main.go

import "github.com/username/package1"
import "github.com/username/package2"
import "github.com/username/package3"

func main() {
    package1.Function1()
    ....
}

然后我可以使用以下命令运行此代码

$ go run main.go

到目前为止我没有问题。但是,如果这个程序有一些带有标志的功能呢?

$ go run main.go  flag1 flag2

这样可以工作,但我想运行类似于

$ myprogram
$ flag1
....
$ flag2

由于所有程序和源代码都将包含在名为myprogram的目录中,该目录也是项目和仓库的名称。

myprogram
----------- package1
----------- package2
----------- package3

我们使用以下命令运行Vim,类似于这样...

$ vim

总结一下,我如何使我的程序通过命令运行,而不是通过go run main?

是否有任何开源仓库可以参考?或者请告诉我要阅读的包或文档。我尝试过

go install

但无法按照我想要的方式工作。

非常感谢!

英文:

This is newbie question... Let's say I have a Go code in a directory(repository initiated here as root) called "myprogram". And I write some packages divided in several sub-directories.

Then I have

repository, root directory

<b>myprogram</b><br>
----------- package1<br>
----------- package2<br>
----------- package3<br>

Then in the myprogram directory, I will write the code with main package for main program and the main program will call all the packages that are defined in the sub-directories, like the following:

main.go

import &quot;github.com/username/package1&quot;
import &quot;github.com/username/package2&quot;
import &quot;github.com/username/package3&quot;

func main() {
    package1.Function1()
    ....
}

Then I can just run this code with

$ go run main.go

I have no problem so far. But what if this program has some features with flags?

$ go run main.go  flag1 flag2

This works, but I want to run something like

$ myprogram
$ flag1
....
$ flag2

Since all the programs and source code will be contained under the directory myprogram which is also the name of the project, repository

<b>myprogram</b><br>
----------- package1<br>
----------- package2<br>
----------- package3<br>

We run Vim with the command, something like this...

$ vim

To summarize, how do I make my program run by command, not by go run main ?

Is there any open source repository that I can refer to? Or please let me know the package or documentation to read. I tried

go install

But can't make it work like I wanted.

Thanks a lot!

答案1

得分: 2

使用go build命令。这将在当前目录中创建一个包的可执行文件。你也可以运行go build <packagename>,它会在$GOPATH/bin目录中创建一个二进制文件。

英文:

Use go build. This will create an executable of the package in the current directory. You can also run go build &lt;packagename&gt;, which creates a binary in $GOPATH/bin.

答案2

得分: 0

请看这个链接

  • 这是一个开源的代码库,供人们参考。
  • 它展示了如何处理主程序具有一些需要不同标志的子功能的情况。
  • 当然,它还展示了如何通过命令行而不是通过"go run main"来运行程序。
  • 最重要的是,通过自动生成所有可以自动生成的内容,使整个过程尽可能简单。
英文:

Take a look at this one

  • it is an open source repository that people can refer to
  • it illustrates how to deal with the cases that the main program can have some sub features that need different flags
  • and for sure it shows how to make programs run by command, not by go run main
  • and most importantly, make the whole process as simple as possible by auto-generate all things that can be auto-generated

huangapple
  • 本文由 发表于 2014年2月21日 11:16:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/21924605.html
匿名

发表评论

匿名网友

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

确定