How to access command-line arguments passed to a Go program?

前端 未结 6 1227
夕颜
夕颜 2020-12-08 01:26

How do I access command-line arguments in Go? They\'re not passed as arguments to main.

A complete program, possibly created by linking m

6条回答
  •  一整个雨季
    2020-12-08 02:18

    You can access the command-line arguments using the os.Args variable. For example,

    package main
    
    import (
        "fmt"
        "os"
    )
    
    func main() {
        fmt.Println(len(os.Args), os.Args)
    }
    

    You can also use the flag package, which implements command-line flag parsing.

提交回复
热议问题