Skip to content

Instantly share code, notes, and snippets.

@IgorDePaula
Created July 31, 2020 21:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IgorDePaula/a81e576734aa53a30d03c33eb1988f33 to your computer and use it in GitHub Desktop.
Save IgorDePaula/a81e576734aa53a30d03c33eb1988f33 to your computer and use it in GitHub Desktop.
go parse execution flags
package main
import (
"flag"
"fmt"
"os"
)
type Car struct {
Model []string
}
var flagname int
var help bool
var count []string
func init() {
flag.IntVar(&flagname, "flagname", 1234, "help message for flagname")
flag.BoolVar(&help, "help", false, "Show this help")
flag.BoolVar(&help, "h", false, "Show this help")
flag.Parse()
if help {
flag.PrintDefaults()
}
count := os.Args
if len(count) == 0 {
flag.PrintDefaults()
os.Exit(1)
}
}
func main() {
c := &Car{}
for i := 0; i < 10; i++ {
c.Model = append(c.Model, "item "+fmt.Sprint(i))
}
fmt.Println(c.Model[5])
fmt.Println(flagname)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment