Global variables / Get command line argument and print it

This may sound silly, but how to define a global variable in Go? const myglobalvariable = "Hi there!" really doesn't work ...

I just want to get the command line argument and after that I want to print it. I do this using this piece of code:

 package main import ( "flag" "fmt" ) func main() { gettext(); fmt.Println(text) } func gettext() { flag.Parse() text := flag.Args() if len(text) < 1 { fmt.Println("Please give me some text!") } } 

The problem is that it just prints an empty string, so I thought of declaring a global variable with const myglobalvariable = "Hi there!" but I just got the error cannot use flag.Args() (type []string) as type ideal string in assignment ... ... I know this is a question about noob, so I hope you can help me. ..

+6
source share
4 answers

Why do you need a global variable? For instance,

 package main import ( "flag" "fmt" ) func main() { text := gettext() fmt.Println(text) } func gettext() []string { flag.Parse() text := flag.Args() if len(text) < 1 { fmt.Println("Please give me some text!") } return text } 
-5
source

I see here at least two questions, maybe three.

  • How do you declare a global variable?
  • How do you declare a global constant?
  • How do you parse command line options and arguments?

Hope the code below demonstrates this in a useful way. The flag pack was one of the first packets I had to cut my teeth in Go. At that time, this was not obvious, although the documentation is improving.

FYI, at the time of this writing, I am using http://weekly.golang.org as a reference. The main site is too outdated.

 package main import ( "flag" "fmt" "os" ) //This is how you declare a global variable var someOption bool //This is how you declare a global constant const usageMsg string = "goprog [-someoption] args\n" func main() { flag.BoolVar(&someOption, "someOption", false, "Run with someOption") //Setting Usage will cause usage to be executed if options are provided //that were never defined, eg "goprog -someOption -foo" flag.Usage = usage flag.Parse() if someOption { fmt.Printf("someOption was set\n") } //If there are other required command line arguments, that are not //options, they will now be available to parse manually. flag does //not do this part for you. for _, v := range flag.Args() { fmt.Printf("%+v\n", v) } //Calling this program as "./goprog -someOption dog cat goldfish" //outputs //someOption was set //dog //cat //goldfish } func usage() { fmt.Printf(usageMsg) flag.PrintDefaults() os.Exit(1) } 
+25
source

The closest thing to a global variable in Go is the package variable. You define one as

 var text string 

The command line arguments, however, are already in the os.Args package variable, expecting you to access them. You do not even need a package of flags.

 package main import ( "fmt" "os" ) func main() { if len(os.Args) < 2 { // (program name is os.Arg[0]) fmt.Println("Please give me some text!") } else { fmt.Println(os.Args[1:]) // print all args } } 
+18
source

See how gofmt , godoc , and others handle the same thing.

0
source

Source: https://habr.com/ru/post/909849/


All Articles