4

【笔记】Go语言获取命令行参数

 1 year ago
source link: https://loli.fj.cn/2023/05/17/Go%E8%AF%AD%E8%A8%80%E8%8E%B7%E5%8F%96%E5%91%BD%E4%BB%A4%E8%A1%8C%E5%8F%82%E6%95%B0/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

【笔记】Go语言获取命令行参数

2023-05-17

Go语言获取命令行参数学习笔记

直接获取命令行参数切片

  • 通过os.Args获取命令行参数切片

从命令行执行程序时所有的命令行参数都会存储在os.Args切片中

for index, value := range os.Args {
...
}

通过键值对获取命令行参数

  • 如果通过可执行文件.exe -u root -p root运行程序,则可以通过flag.StringVar()方法无视顺序获取-key后面的值
import flag
var username string
flag.StringVar(&username, "u", "默认值", "对参数的描述")
var password string
flag.StringVar(&password, "p", "默认值", "对参数的描述")
flag.Parse()

哔哩哔哩——尚硅谷


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK