Created
July 31, 2020 21:50
-
-
Save IgorDePaula/a81e576734aa53a30d03c33eb1988f33 to your computer and use it in GitHub Desktop.
go parse execution flags
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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