Подтвердить что ты не робот

Как получить доступ к аргументам командной строки, переданным программе Go?

Как мне получить доступ к аргументам командной строки в Go? Они не передаются как аргументы main.

Полная программа, возможно созданная путем связывания нескольких пакетов, должна иметь один пакет main, с функцией

func main() { ... }

определена. Функция main.main() не принимает аргументов и не возвращает значения.

4b9b3361

Ответ 1

Вы можете получить доступ к аргументам командной строки, используя переменную os.Args. Например,

package main

import (
    "fmt"
    "os"
)

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

Вы также можете использовать пакет флага , который реализует разбор флагов командной строки.

Ответ 2

Аргументы командной строки можно найти в os.Args. В большинстве случаев пакет flag лучше, потому что он анализирует аргумент для вас.

Ответ 3

Ответ Peter - это именно то, что вам нужно, если вам просто нужен список аргументов.

Однако, если вы ищете функциональность, подобную той, что присутствует в UNIX, вы можете использовать go реализацию docopt. Вы можете попробовать здесь.

docopt вернет JSON, после чего вы сможете обработать ваш сердечный контент.

Ответ 4

Флаг - хороший пакет для этого.

// [_Command-line flags_](http://en.wikipedia.org/wiki/Command-line_interface#Command-line_option)
// are a common way to specify options for command-line
// programs. For example, in `wc -l` the `-l` is a
// command-line flag.

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 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())
}