title: What's Go's equivalent of argv[0]?
body: <p>How can I get my own program's name at runtime? What's Go's equivalent of C/C++'s argv[0]? To me it is useful to generate the usage with the right name.</p>
  
  <p>Update: added some code.</p>
  
  <pre><code>package main
  
  import (
      "flag"
      "fmt"
      "os"
  )
  
  func usage() {
      fmt.Fprintf(os.Stderr, "usage: myprog [inputfile]\n")
      flag.PrintDefaults()
      os.Exit(2)
  }
  
  func main() {
      flag.Usage = usage
      flag.Parse()
  
      args := flag.Args()
      if len(args) &lt; 1 {
          fmt.Println("Input file is missing.");
          os.Exit(1);
      }
      fmt.Printf("opening %s\n", args[0]);
      // ...
  }
  </code></pre>