当前位置:网站首页>[golang learning notes] simple use of flag package, command line parsing
[golang learning notes] simple use of flag package, command line parsing
2022-07-23 22:05:00 【Vivien_ oO0】
List of articles
flag The package implements the parsing of command line parameters
This article is just a simple introduction , Details please see Official documents
func String
func String(name string, value string, usage string) *string
String Use the specified name 、 The default value is 、 Use the information to register a string type flag. Return a saved flag Pointer to the value of .
package main
import (
"flag"
"fmt"
)
// The first parameter is zero name The second parameter is the default value The third parameter is description
// Note that the accepted variable is a pointer
var mode = flag.String("mode", "", "process mode")
func main() {
// To analyze
flag.Parse()
// Output
fmt.Println(*mode)
}
Use... When executing --name=value To pass
Command line flag grammar :
-flag
-flag=x
-flag x // Only non bool Type of flag Sure bool Type used = assignment
One - And two - It's the same
for example :
go run main.go --mode=hello
func StringVar
func StringVar(p *string, name string, value string, usage string)
StringVar Use the specified name 、 The default value is 、 Use the information to register a string type flag, And will flag The value of is saved to p Variable pointed to .
Code :
package main
import (
"flag"
"fmt"
)
func main() {
var str string
fmt.Println(" Start parsing ")
flag.StringVar(&str, "str", "", "parse string")
// Analytical parameters
flag.Parse()
// Output
fmt.Println(str)
Command line input :
go run test1.go -str="hello go"
result :
Start parsing
hello go
flag The command line parameter types supported by the package are bool、int、int64、uint、uint64、float float64、string、duration.
| flag Parameters | Valid values |
|---|---|
| character string flag | Legal string |
| Integers flag | 1234、0664、0x1234 Other types , It can also be negative . |
| Floating point numbers flag | Legal floating point number |
| bool type flag | 1, 0, t, f, T, F, true, false, TRUE, FALSE, True, False. |
| Period of time flag | Any legal time period string . Such as ”300ms”、”-1.5h”、”2h45m”. Legal units include ”ns”、”us” /“µs”、”ms”、”s”、”m”、”h”. |
边栏推荐
- STM32+ESP8266+MQTT协议连接阿里云物联网平台
- Redis common commands correspond to redisson object operations
- 【golang学习笔记】包(package)的使用
- A stack of digital robots were selected in Gartner's China AI market guide
- Real time monitoring of MySQL database changes_ Synchronize data_ Learn about canal_--- Canal work notes 001
- I, AI doctoral student, online crowdfunding research topic
- Improving performance with explicit rendering
- lambda学习(sort后面的Comparator的使用,collection后使用Collectors.groupingBy分组)
- Programmation JDBC pour MySQL
- Principle and implementation of hash table, unordered set and mapping
猜你喜欢

What are the product life cycle, common project functions, and information flow

MySQL索引事务

【HiFlow】腾讯云新一代自动化助手,我用它完成了企业疫情提示(无代码)

性能测试知识应用于实战

Lambda learning (the use of comparator after sort and collectors.groupingby after collection)

【golang学习笔记】Go语言中参数的传递是值传递还是引用传递

prime_ series_ level-1

【数学建模暑期培训】配送中心选址问题

-2021 sorting and sharing of the latest required papers related to comparative learning

LeetCode高频题53. 最大子数组和,具有最大和的连续子数组,返回其最大和
随机推荐
Comparison of open source distributed link tracking
Still have 1 requests outstanding when connection from slaveX/X.X.X.X:33202 is closed
Jedis 6 - Introduction and difference between redisson and jedis
【AcWing】周赛
Openlayers instances advanced mapbox vector tiles advanced mapbox vector maps
Uniapp uses canvas to write a circular progress bar
Complete set of official openlayers instances
lambda学习(sort后面的Comparator的使用,collection后使用Collectors.groupingBy分组)
Comment forcer complètement le meurtre de processus indépendants de l'arrière - plan?
U++ 学习笔记 控制物体Scale
U++学习笔记 TSubclassOf()
Sudoku written for once and for all
10 basic written interview questions, how many questions can you answer?
Altium Designer - schematic diagram of Arduino uno & PCB diagram (self-made Arduino board)
MySQL index transaction
Yuanqi Digitalization: existing mode or open source innovation Lixia action
02.网页结构相关知识补充
MySQL的JDBC編程
I, AI doctoral student, online crowdfunding research topic
[mathematical modeling summer training] location of distribution center