社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
switch 接收命令行参数
package main
import (
"fmt"
"os"
)
func main() {
// os.Args[0] 为程序带路径的名称
// os.Args[1] 为第一个参数
args := os.Args
if len(args) < 2 {
fmt.Println("param error.")
}
switch args[1] {
case "hello":
fmt.Println("hello")
case "world":
fmt.Println("world")
default:
fmt.Println("default")
}
}
测试
go build -o test.exe switch.go
./test.exe hello world wu test
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!