首页 > 其他 > 详细

Go-15-flag.String 获取系统参数

时间:2020-05-26 22:46:41      阅读:47      评论:0      收藏:0      [点我收藏+]

场景:

启动应用程序时,需要传入系统参数。例如:./start --b /notebook --p true --n 8

package main

import (
    "fmt"
    flag "github.com/spf13/pflag"
)

func main() {
    home_dir:= flag.String("b","/home/default_dir","home path") 
    isProdEnvironment:= flag.Bool("p", false,"environment is pord")
    int_value:= flag.Int("n",2, "pod num")

    flag.Parse()

    fmt.Println("backup_dir:",*home_dir)
    fmt.Println("isProdEnvironment",*isProdEnvironment)
    fmt.Println("int_value",*int_value)
}

运行结果:

D:\GoWorkspace\my-go-code\test>go run Test8.go --b "/home/back" --p true --n 8
backup_dir: /home/back
isProdEnvironment true
int_value 8

D:\GoWorkspace\my-go-code\test>

其中:

// String defines a string flag with specified name, default value, and usage string.
// The return value is the address of a string variable that stores the value of the flag.
func String(name string, value string, usage string) *string {
    return CommandLine.StringP(name, "", value, usage)
}

 

Go-15-flag.String 获取系统参数

原文:https://www.cnblogs.com/shix0909/p/12968477.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!