snippetgoCritical
How to access command-line arguments passed to a Go program?
Viewed 0 times
programhowaccesslinecommandpassedarguments
Problem
How do I access command-line arguments in Go? They're not passed as arguments to
A complete program, possibly created by linking multiple packages, must have one package called main, with a function
defined. The function main.main() takes no arguments and returns no value.
main.A complete program, possibly created by linking multiple packages, must have one package called main, with a function
func main() { ... }defined. The function main.main() takes no arguments and returns no value.
Solution
You can access the command-line arguments using the
You can also use the flag package, which implements command-line flag parsing.
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.
Code Snippets
package main
import (
"fmt"
"os"
)
func main() {
fmt.Println(len(os.Args), os.Args)
}Context
Stack Overflow Q#2707434, score: 160
Revisions (0)
No revisions yet.