duanqilupinf67040 2014-02-21 03:16
浏览 70
已采纳

Go,Golang:程序,命令行界面的程序包结构

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

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

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 "github.com/username/package1"
import "github.com/username/package2"
import "github.com/username/package3"

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

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

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条回答 默认 最新

  • dongtang1966 2014-02-21 03:20
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?