当前位置:网站首页>os.Args[1:]中命令行参数为空时,不执行内部语句
os.Args[1:]中命令行参数为空时,不执行内部语句
2022-06-22 20:53:00 【Jimmy_jimi】
问题描述
当命令行中输入值为空时,不会执行else后面的语句块.
func qualityTran() {
for _,v := range os.Args[1:]{
//如果不为空,直接对原值转换
if v !=""{
a,_ := strconv.Atoi(v)
fmt.Println(tran(a))
//input.Scan()
}else {
fmt.Println("1")
//如果是空则从标准输入读取
fmt.Println("please input a num:")
input := bufio.NewScanner(os.Stdin)
input.Scan()
a :=input.Text()
b,_ := strconv.Atoi(a)
fmt.Println(tran(b))
}
}
}
原因分析:
实际再程序执行过程中,for循环直接结束,并不会进入到if语句判断.
解决方案:
func qualityTran() {
flag :=0
for _,v := range os.Args[1:]{
//如果不为空,直接对原值转换
if v !=""{
a,_ := strconv.Atoi(v)
fmt.Println(tran(a))
//input.Scan()
flag =1
}
}
if flag ==0 {
//如果是空则从标准输入读取
fmt.Println("please input a num:")
input := bufio.NewScanner(os.Stdin)
input.Scan()
a :=input.Text()
b,_ := strconv.Atoi(a)
fmt.Println(tran(b))
}
}
边栏推荐
- Phantomjs utility code snippet (under continuous update...)
- What are the indicators, dimensions and models in Business Intelligence BI data warehouse?
- How to change the dial on the apple Watch
- Considerations for using redisson to operate distributed queues
- Greedy interval problem (3)
- node-fetch下载文件
- 斐波那契数列合集
- Ensure database and cache consistency
- [redisson] source code analysis of multilock
- . Net 5.0 realizes the source code analysis of the oidc authentication part of single sign on through identityserver4
猜你喜欢

保证数据库和缓存的一致性

Is it bad for NFT that the market starts to cool down?

Experiment 4 operation comparison between NoSQL and relational database

2021-04-14

The mental process and understanding of visual project code design

2021-04-14

LinkedList source code analysis
Several ways of redis persistence -- deeply parsing RDB

Why is yuancosmos so popular? Is the 10trillion yuan shouted by the market boasting or the truth?

Plan and change of continuous repair
随机推荐
ArcGIS application (20) the ArcGIS grid image symbol system prompts "this dataset does not have valid histogram required for classificati..."
Palindromes (simple version)
2021-05-02
2021-04-14
Relationship between adau1452 development system interface and code data
swagger2 使用方法
2021-03-06
Enabling partners, major guarantee of Spring Festival "non-stop"
2021-07-27
js----SVG转PNG
安装typescript环境并开启VSCode自动监视编译ts文件为js文件
Three cache methods and principles
2021-08-22
2020-12-04
Codeup longest palindrome substring
Learn redis with you (11) -- redis distributed lock
Explain the startup process of opengauss multithreading architecture in detail
2021-03-06
Valid parentheses
R language data preprocessing, converting type variables into factor variables, converting data sets into H2O format, and dividing data sets (training set, test set, verification set)