os包地址:https://pkg.go.dev/os@go1.19.5
os.Args
返回值为 var Args []string
通过Args,可以在命令行内输入一些参数
(默认情况下os.Args[0]为可执行文件的路径)
[包作用:Package os provides a platform-independent interface to operating system functionality. The design is Unix-like, although the error handling is Go-like; failing calls return values of type error rather than error numbers. Often, more information is available within the error. For example, if a call that takes a file name fails, such as Open or Stat, the error will include the failing file name when printed and will be of type *PathError, which may be unpacked for more information.
The os interface is intended to be uniform across all operating systems. Features not generally available appear in the system-specific package syscall.]
flag包地址:https://pkg.go.dev/flag@go1.19.5
1.flag.IntVar()
函数详解:func IntVar(p *int, name string, value int, usage string)
通过IntVar,可以在命令行中借助 -name + arg 的形式传参,并赋值给p
[包地址:https://pkg.go.dev/flag]
[包作用:Package flag implements command-line flag parsing.]
2.flag.StringVar()
函数详解:func StringVar(p *string, name string, value string, usage string)
3.flag.BoolVar()
函数详解:func BoolVar(p *bool, name string, value bool, usage string)
flag.Parse()
flag.Usage()
关于function Usage():
关于 func PrintDefaults():
--------------------------------
flag包总结:以上3个方法,均通过value设置p的默认值,通过-name在【命令行】内输入新的p值,而usage则是对p的用途进行说明。此外,针对bool类型的p,只要在命令行中附上 -name,该值即为true