In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to use the main function with parameters in the Go language. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
Method 1: os.Args implementation
Let's write a main function, where os.Args is a [] string type. When you run the main function, the parameters on the command line are written to the os.Args data.
However, such parameters cannot specify what type or what tag is, for example:-t,-h, and so on.
1. Put os.Args directly into the main function
Output1:
$. / args1
Output2:
$. / args1-y-t-o 0000000
Note: the output result can be found in os.Args, and the executed command. / args1 will also be placed in os.Args [0].
two。 For the os.Args implementation in argsfunc ()
Os.Args can be used not only in the main function, but also in other functions, so os.Args is more like a global variable. It should have written the parameters on the command line to os.Args before main.
Output:
$. / args 0 000 1 20 30
Note: as you can see from the output, the parameters implemented in the main function are no different from the os.Args implemented in the argsfunc function.
Method 2: flag package implementation
The flag package has more powerful support for parameters, specifying not only the tag of the parameters (for example:-h,-t, etc.), but also the type of each parameter.
When using flag, first, declare the type and format of the parameter; second, you need to call flag.Parse () before using the parameter, which should parse the parameter list on the command line into the parameter format of your life.
Output 1: / / just execute the executable file
$. / args2
$. / args2-b-s hello-t 10 the other args
Note: from the output result, we can see that after flag.Parse () parsing, the. / args is not taken as the first parameter, just like the data read from os.Args [1:], but not the data of os.Args [0].
So much for sharing about how to use the main function with parameters in the GE language. I hope the above content can be of some help and learn more knowledge. If you think the article is good, you can share it for more people to see.
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.