Go编译、运行和执行二进制的行命令

生成安装包

系统执行
go build  (-x 可以看到具体都执行了哪些操作)
混编
#如果我们想生成linux和windows上的程序,只要通过一下命令:
$gox -os "windows linux" -arch amd64

  1.编译window 64位:
  	gox -osarch="windows/amd64" ./
  2.编译mac 64位:
  	gox -osarch = "darwin/amd64" ./
  3.编译Linux 64位:
  	gox -osarch="linux/amd64" ./

执行命令包

  1. linux(./main 编译后的文件可执行采集 ./main & 后台运行)
  2. Windows(直接运行main.exe GoModTest.exe -name 123)
注意:
现象:同一个文件夹下面有多个go文件,a.go,b.go,c.go,其中main在a.go中,直接go run a.go,报undefined 错误12
原因:go在run之前会先进行编译操作,而在此处的编译它只会以这个a.go为准,导致其他几个引用文件中的方法出现找不到的情况
(而采用go build的方式又不一样,他会自动查找引用文件并打包)123
解决办法:go run a.go b.go c.go

(重点)自定义交互命令

1.指定加载的配置文件

./main  -c  指定加载的配置文件

2.例如获取版本号-v

[root@hu mq]# ./mqbeat_linux_amd64 -v
 1.0.0

main.go代码如下

if util.SliceContainString(os.Args, "-v") {
	fmt.Printf("%v\n", config.Version)
	os.Exit(0)
}

os.Args作为数组的输出里面可以取到具体的值

E:\Workplace_Go\src\GoModTest>go run main.go -v 200
[C:\Users\Admin\AppData\Local\Temp\go-build184099962\b001\exe\main.exe -v 200]

3.获取帮助信息-h

[root@hu mq]# ./mqbeat_linux_amd64 -h
Usage of Websphere_MQbeat:
  -E value
    	Configuration overwrite (default null)
  -N	Disable actual publishing for testing
  -T	test mode with console output
  -c value
    	Configuration file, relative to path.config (default beat.yml)
  -configtest
    	Test configuration and exit.
  -cpuprofile string
    	Write cpu profile to file
  -httpprof string
    	Start pprof http server
  -memprofile string
    	Write memory profile to this file
  -path.config value
    	Configuration path
  -path.data value
    	Data path
  -path.home value
    	Home path
  -path.logs value
    	Logs path
  -reload
    	reload config
  -setup
    	Load the sample Kibana dashboards
  -strict.perms
    	Strict permission checking on config files (default true)
  -version
    	Print the version and exit
Informations:
  Version= 1.0.0
  BuildCommit= 
  BuildTime= 2019/06/26
  GoVersion= 1.12.1

main.go代码如下

	if util.SliceContainString(os.Args, "-h") {
		buf := bytes.NewBufferString("Usage of " + config.AppName + ":\n")
		flag.CommandLine.SetOutput(buf)
		flag.PrintDefaults()
		fmt.Printf("%v", buf.String())
		fmt.Println("Informations:")
		fmt.Printf("  Version= %v\n", config.Version)
		fmt.Printf("  BuildCommit= %v\n", config.BuildCommit)
		fmt.Printf("  BuildTime= %v\n", config.BuildTime)
		fmt.Printf("  GoVersion= %v\n", config.GoVersion)
		os.Exit(0)
	}

config.go代码如下

var (
	AppName     = "MQbeat"
	Version     = "1.0.0"
)
交互方式命令行参数Flag:

在 go 标准库中提供了一个包:flag,方便进行命令行解析

1.flag.Parse()要放在main函数体的第一行

var name string
flag.StringVar(&name, "name", "everyone", "The greeting object.")
flag.Parse()
fmt.Println(name)

flag.BoolVar(&h, "h", false, "this help")
flag.BoolVar(&v, "v", false, "show version and exit")

flag.Int、flag.Bool、flag.String这样的函数格式都是一样的。
参数的说明如下:
&name:为接收到的值,具体指为name。
第一个arg表示参数名称,在控制台的时候,提供给用户使用。
第二个arg表示默认值,如果用户在控制台没有给该参数赋值的话,就会使用该默认值。
第三个arg表示使用说明和描述,在控制台中输入-arg的时候会显示该说明,类似-help。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值