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

Go

Go Problem Overview


How do I access command-line arguments in Go? They're not passed as arguments to 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.

Go Solutions


Solution 1 - Go

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.

Solution 2 - Go

Flag is a good package for that.

package main

// Go provides a `flag` package supporting basic
// command-line flag parsing. We'll use this package to
// implement our example command-line program.
import "flag"
import "fmt"

func main() {

    // Basic flag declarations are available for string,
    // integer, and boolean options. Here we declare a
    // string flag `word` with a default value `"foo"`
    // and a short description. This `flag.String` function
    // returns a string pointer (not a string value);
    // we'll see how to use this pointer below.
    wordPtr := flag.String("word", "foo", "a string")

    // This declares `numb` and `fork` flags, using a
    // similar approach to the `word` flag.
    numbPtr := flag.Int("numb", 42, "an int")
    boolPtr := flag.Bool("fork", false, "a bool")

    // It's also possible to declare an option that uses an
    // existing var declared elsewhere in the program.
    // Note that we need to pass in a pointer to the flag
    // declaration function.
    var svar string
    flag.StringVar(&svar, "svar", "bar", "a string var")

    // Once all flags are declared, call `flag.Parse()`
    // to execute the command-line parsing.
    flag.Parse()

    // Here we'll just dump out the parsed options and
    // any trailing positional arguments. Note that we
    // need to dereference the pointers with e.g. `*wordPtr`
    // to get the actual option values.
    fmt.Println("word:", *wordPtr)
    fmt.Println("numb:", *numbPtr)
    fmt.Println("fork:", *boolPtr)
    fmt.Println("svar:", svar)
    fmt.Println("tail:", flag.Args())
}

Solution 3 - Go

Command line arguments can be found in os.Args. In most cases though the package flag is better because it does the argument parsing for you.

Solution 4 - Go

Quick Answer:

package main

import ("fmt"
	    "os"
)

func main() {
	argsWithProg := os.Args
	argsWithoutProg := os.Args[1:]
	arg := os.Args[3]
	fmt.Println(argsWithProg)
	fmt.Println(argsWithoutProg)
	fmt.Println(arg)
}

Test: $ go run test.go 1 2 3 4 5

Out:

[/tmp/go-build162373819/command-line-arguments/_obj/exe/modbus 1 2 3 4 5]
[1 2 3 4 5]
3

> NOTE: os.Args provides access to raw command-line arguments. Note that the first value in this slice is the path to the program, > and os.Args[1:] holds the arguments to the program. > Reference

Solution 5 - Go

Peter's answer is exactly what you need if you just want a list of arguments.

However, if you're looking for functionality similar to that present on UNIX, then you could use the go implementation of docopt. You can try it here.

docopt will return JSON that you can then process to your heart's content.

Solution 6 - Go

you can use the Golang flag package for example,

package main

import (
	"flag"
	"fmt"
)

func main() {

	wordPtr := flag.String("word", "default value", "a string for description")
	flag.Parse()
	fmt.Println("word:", *wordPtr)

}

call with cli

 go run main.go -word=hello
 
 

output

word: hello

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOleg RazgulyaevView Question on Stackoverflow
Solution 1 - GopeterSOView Answer on Stackoverflow
Solution 2 - GoBufBillsView Answer on Stackoverflow
Solution 3 - GoMaurice GildenView Answer on Stackoverflow
Solution 4 - GoBenyamin JafariView Answer on Stackoverflow
Solution 5 - GoCarlView Answer on Stackoverflow
Solution 6 - Godılo sürücüView Answer on Stackoverflow