Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use flag package in Go language

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the knowledge of "how to use flag package in Go language". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Os.Args

In fact, the os module of Go also has this function, but this function is a little lacking.

You cannot receive a value that specifies key like-u root-p 3306, only a way like root 3306. Let's get to know it briefly.

Code

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

We compiled into a mysql.exe with the command go build-o mysql.exe main.go.

Execution result

Note: the returned cmdArgs is a slice, and the first value is your own file name. If you don't want your first value, just slice it.

Modify line 2 of the code.

CmdArgs: = os.Args [1:]

Execution result

Flag

The above may also find a problem, can only receive simple values, can not receive key value such.

Flag can do this, and flag can only accept the following types.

Bool

Int Series (int,int64,uint,uint64)

Float Series (float,float64)

String

Duration

Be careful

With the flag package, you end up with a conversion using flag.Parse () to get the command-line arguments.

Flag.Type ()

Don't say much, just code.

Func main () {/ / the first parameter is command line key, the second parameter is default, and the third parameter is .exe-h prompt var user = flag.String ("user", "root", "user name") var port = flag.Int ("port", 3306, "port") var ip = flag.String ("ip", "localhost") "mysql ip") / / you must use flag.Parse () to parse the command line parameter flag.Parse () / / flag.Type, which returns a pointer, which must be evaluated by * variable fmt.Println (* user,*port,*ip)}

Execution result

.exe-h result diagram

I see, today, have you failed your study?

Flag.TypeVar ()

You may also find a problem with the above. You need to use the * variable to get the value. If it feels inconvenient, take a look at flag.TypeVar ().

Func main () {/ / declare variable to receive command line parameter var user string var port int var ip string / / from command line scan parameter assignment to variables flag.StringVar (& user, "user", "root", "user name") flag.IntVar (& port, "port", 3306, "port") flag.StringVar (& ip, "ip", "localhost") "mysql ip") / / you must use flag.Parse () to parse the command line parameter flag.Parse () / / flag.Type, which returns a pointer and must take the value fmt.Println (user, port, ip)} through * variable.

Execution result

Flag other methods

Flag has some other unimportant methods, which is good to understand.

Func main () {/ / declare variable to receive command line parameter var user string var port int var ip string / / from command line scan parameter assignment to variables flag.StringVar (& user, "user", "root", "user name") flag.IntVar (& port, "port", 3306, "port") flag.StringVar (& ip, "ip", "localhost") "mysql ip") / / you must use flag.Parse () to parse the command line parameter flag.Parse () / / flag.Type, which returns a pointer and must take the value fmt.Println (user, port) through the * variable. Ip) / other methods / / other parameters after returning command line parameters fmt.Println (flag.Args ()) / / number of other parameters after returning command line parameters fmt.Println (flag.NArg ()) / / number of command line parameters used fmt.Println (flag.NFlag ())}

Execution result

This is the end of the content of "how to use flag packages in the Go language". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report