Golang flag.FlagSet.String() function example
package flag
String defines a string flag with specified name(1st parameter), default value(2nd parameter), and usage string(3rd parameter). The return value is the address of a string variable that stores the value of the flag.
Golang flag.FlagSet.String() function usage example
package main
import (
"flag"
"fmt"
)
var (
flagSet *flag.FlagSet
p string
)
func main() {
flagSet = flag.NewFlagSet("example", flag.ContinueOnError)
filename := flagSet.String("file", "textfile.txt", "filename to view")
flagSet.StringVar(&p, "timeout", "5000", "time to wait for response")
err := flagSet.Parse([]string{"file", "timeout"})
if err != nil {
fmt.Println(err)
}
fmt.Println(*filename)
fmt.Println(p)
}
Output :
textfile.txt
5000
Reference :
Advertisement
Something interesting
Tutorials
+6.4k CodeIgniter : form input set_value cause " to become & quot
+7.7k Golang : Mapping Iban to Dunging alphabets
+9.7k Golang : Eroding and dilating image with OpenCV example
+6.9k Default cipher that OpenSSL used to encrypt a PEM file
+14.4k Golang : How to convert a number to words
+14.8k Golang : Get URI segments by number and assign as variable example
+48.1k Golang : How to convert JSON string to map and slice
+5.7k Get website traffic ranking with Similar Web or Alexa
+8.3k Golang: Prevent over writing file with md5 hash
+7.5k Golang : Gorrila set route name and get the current route name
+5.3k Golang : Pad file extension automagically