golang error、flag 15
程序员文章站
2022-07-14 17:09:36
...
error
var write = errors.New("分母不能为0")
/*
通用处理方式
*/
func run(a int)(int,error) {
if a == 0 {
return 0,write
}
return 66 / a, nil
}
func main() {
v, error := run(0)
if error != nil {
fmt.Println(v,error)
return
}
fmt.Println(v)
}
flag
/*相当于设置的默认值*/
var Input_Name = flag.String("name", "dabaojian", "input your name")
var Input_Age = flag.Int("age", 20, "input your age")
var Input_Gender = flag.String("gender", "male", "input your age")
var Input_flagvar int
func Init2() {
flag.IntVar(&Input_flagvar, "flagname", 1234, "help message for flagname")
}
/**
go build flag.go 编译:
./flag -name=world 运行:
下面就能获取到运行时设置的参数,如果没设置,就是用默认值
*/
func main() {
Init2()
flag.Parse() //通过启动参数 获取flag的值
//
//fmt.Printf("args=%s, num=%d\n", flag.Args(), flag.NArg())
//fmt.Printf("flag num=%d\n", flag.NFlag())
//
//for i := 0; i != flag.NArg(); i++ {
// fmt.Printf("arg[%d]=%s\n", i, flag.Arg(i))
//}
fmt.Println("name=", *Input_Name)
fmt.Println("age=", *Input_Age)
fmt.Println("gender=", *Input_Gender)
fmt.Println("flagname=", Input_flagvar)
}
推荐阅读
-
Golang error 的突围
-
MindManager 15中文版怎么解决runtime error?
-
golang自定义error
-
[Go] golang的error接口
-
VMWare 15 Error on Ubuntu 18.4 - Could not open /dev/vmmon: No such file or directory
-
tensorflow error :UnrecognizedFlagError: Unknown command line flag ‘f‘ 解决办法
-
xdd-plus-fetch qrcode error: Packet timed out,2-error: unknown flag ,升级xdd1.7版本解决
-
tensorflow的 jupyter环境 error :UnrecognizedFlagError: Unknown command line flag ‘f‘ 解决办法
-
golang error、flag 15
-
navicat15 RA-28547:connection to server failed, probable Oracle Net admin error