Golang flag.FlagSet.StringVar() function example
package flag
StringVar defines a string flag with specified name(2nd parameter), default value(3rd parameter), and usage string(4th parameter). The argument p(1st parameter) points to a string variable in which to store the value of the flag.
Golang flag.FlagSet.StringVar() 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") // <-- here
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
+5.2k Golang : Convert lines of string into list for delete and insert operation
+4.7k Javascript : Access JSON data example
+8.8k Golang : HTTP Routing with Goji example
+5.9k Unix/Linux : How to open tar.gz file ?
+14.5k Golang : Overwrite previous output with count down timer
+24.6k Golang : How to print rune, unicode, utf-8 and non-ASCII CJK(Chinese/Japanese/Korean) characters?
+12.6k Golang : Get absolute path to binary for os.Exec function with exec.LookPath
+10.6k RPM : error: db3 error(-30974) from dbenv->failchk: DB_RUNRECOVERY: Fatal error, run database recovery
+7.5k Golang : How to handle file size larger than available memory panic issue
+26.8k Golang : Convert file content into array of bytes
+12.9k Golang : Convert IPv4 address to packed 32-bit binary format
+9.4k Android Studio : Indicate progression with ProgressBar example