Home  >  Article  >  Backend Development  >  An article will guide you through the use of flag package in Go language

An article will guide you through the use of flag package in Go language

Go语言进阶学习
Go语言进阶学习forward
2023-07-20 14:07:591487browse
This time I will talk about one of the more commonly used packages in the Go languageflag package.

#The purpose of this package is to receive command line parameters.

If you have playedMysql, you must be familiar with this interface

An article will guide you through the use of flag package in Go language

Yes, that’s what it’s used for, receiving command line parameters.


Note

Any code learned in this chapter must be compiled into .exe can run, prohibit right click run! !

An article will guide you through the use of flag package in Go language


##os.Args

In fact Go The os module also has this function, but this function is somewhat lacking.

Cannot receive a specification like -u root -p 3306 The value of key, can only receive the value like root 3306. Let’s take a brief look.

Code

func main() {
    cmdArgs := os.Args
    if len(cmdArgs) > 0 {
        for index, value := range cmdArgs {
            fmt.Printf("索引:%v,value值:%v\n", index, value)
        }
}
}

We pass the commandgo build -o mysql. exe main.go is compiled into a mysql.exe.

An article will guide you through the use of flag package in Go language

##mysql.exe diagram

An article will guide you through the use of flag package in Go language

Execution results

An article will guide you through the use of flag package in Go language

##Note:Returned cmdArgs is a slice, the first value is its own file name , if you don’t want your first value, just slice it directly.

Modify the

line 2 code.

cmdArgs := os.Args[1:]

执行结果

An article will guide you through the use of flag package in Go language


flag

上述的可能也发现问题了,只能接收简单的值,不能接收key value这种。

flag可以做到,flag有只能接收以下几种类型

  • bool

  • intSeries(int, int64, uint, uint64 )

  • float series(float ,float64)

  • string

  • duration

注意

使用flag包,最后要使用flag.Parse()转换一下才能获取命令行参数。

flag.Type()

话不多说,直接代码

func main() {
    // 第一个参数是命令行key,第二个参数是默认是,第三个参数是 .exe -h 提示
    var user = flag.String("user", "root", "用户名")
    var port = flag.Int("port", 3306, "端口")
    var ip = flag.String("ip", "localhost", "mysql ip")

    //必须使用flag.Parse()解析一下命令行参数
    flag.Parse()
    //flag.Type返回的是一个指针,必须通过 *变量取值
    fmt.Println(*user,*port,*ip)
}

执行结果

An article will guide you through the use of flag package in Go language

.exe -h结果图

An article will guide you through the use of flag package in Go language

懂了叭,你今天,学废了吗!

flag.TypeVar()

上述你可能也发现了问题,需要用*变量才能取到值,是不是感觉不太方便,那就来看看flag.TypeVar()

func main() {
    //声明变量用于接收命令行参数
    var user string
    var port int
    var ip string

    //从命令行扫描参数赋值到变量
    flag.StringVar(&user, "user", "root", "用户名")
    flag.IntVar(&port, "port", 3306, "端口")
    flag.StringVar(&ip, "ip", "localhost", "mysql ip")

    //必须使用flag.Parse()解析一下命令行参数
    flag.Parse()
    //flag.Type返回的是一个指针,必须通过 *变量取值
    fmt.Println(user, port, ip)
}

执行结果

An article will guide you through the use of flag package in Go language

flag其他方法

flag还有一些其他无关紧要的方法,了解就好。

func main() {
    //声明变量用于接收命令行参数
    var user string
    var port int
    var ip string

    //从命令行扫描参数赋值到变量
    flag.StringVar(&user, "user", "root", "用户名")
    flag.IntVar(&port, "port", 3306, "端口")
    flag.StringVar(&ip, "ip", "localhost", "mysql ip")

    //必须使用flag.Parse()解析一下命令行参数
    flag.Parse()
    //flag.Type返回的是一个指针,必须通过 *变量取值
    fmt.Println(user, port, ip)
    ///////////////// 其他方法
    //返回命令行参数后的其他参数
    fmt.Println(flag.Args())
    //返回命令行参数后的其他参数个数
    fmt.Println(flag.NArg())
    //返回使用的命令行参数个数
    fmt.Println(flag.NFlag())
}

执行结果

An article will guide you through the use of flag package in Go language


Summary

The main thing to learn in this chapter is the flag package Use, #flag package mainly handles the command line parameter passing problem.

With this we can develop programs like mysql.

flag Mainly include flag.Type and flag.TypeVarTwo differences

  • flag.TypeThe variable coming out is a pointer type, you need to use *Variable namecan take the value

  • ##flag.TypeVarThe variable that comes out is directly the standard variable (recommend)

The above is the detailed content of An article will guide you through the use of flag package in Go language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:Go语言进阶学习. If there is any infringement, please contact admin@php.cn delete