Last active
August 29, 2015 14:03
-
-
Save benbjohnson/4db72a84b744325c9603 to your computer and use it in GitHub Desktop.
flags
This file contains hidden or 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" | |
| "log" | |
| "github.com/benbjohnson/myapp" | |
| ) | |
| func main() { | |
| // Parse flags. | |
| connstr := flag.String("c", "", "connection string") | |
| addr := flag.String("addr", "", "bind address") | |
| num := flag.Int("num", 100, "number of things") | |
| flag.Parse() | |
| // Validate flags here for prettier error messages. | |
| // Usually I'll just defer validation to the objects | |
| // which use the flags though. | |
| if *c == "" { | |
| log.Fatal("connection string required: -c") | |
| } else if *addr == "" { | |
| log.Fatal("bind address required: -addr") | |
| } else if *num < 0 { | |
| log.Fatal("num must be positive") | |
| } | |
| // Create my database. | |
| var db myapp.DB | |
| if err := db.Open(*connstr); err != nil { | |
| log.Fatal("db: ", err) | |
| } | |
| defer db.Close() | |
| // Create my server and attach the database. | |
| s, err := myapp.NewServer(db, *addr) | |
| if err != nil { | |
| log.Fatal("server: ", err) | |
| } | |
| defer s.Close() | |
| log.Fatal(s.ListenAndServe()) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment