What's Go's equivalent of argv[0]? -
How do I get my own program name at runtime? Is the argv [0] of C / C ++ equals? For me it is useful to generate experimentation with the correct name.
Update: Added some code. Package main import ("flag" "FMT" "OS") Func usage () {fmt.Fprintf (os.Stderr, "use: myprog [inputfile] \ n") flag. PrintDefaults () os.Exit (2)} func main () {flag.Usage = use flag.Parse () Args: = flag.Args () if lane (args) & lt; 1 {fmt.Println ("The input file is unavailable."); Os.Exit (1); } Fmt.Printf ("Open% s \ n", args [0]); // ...}
import "OS" OSARG [0 ] // The name of the command that is running as OSARG [1] / first command line parameter ...
logic os
package
If you are going to deal with logic, then the flag
package is the preferred method especially for your case flag.use
Update for the example you provided:
func usage () {fmt.Fprintf (os.Stderr, "Usage:% s [inputfile] \ n", os.Args [0 ] Flag. Print Default (OS) (2)}
should do the trick
Comments
Post a Comment