flag

Fill in the blanks in line A and B, to parse terminal arguments to ip and port, which default to 0.0.0.0 and 8000.

  1. package main
  2. import "flag"
  3. import "fmt"
  4. var ip string
  5. var port int
  6. func init() {
  7. // A
  8. // B
  9. }
  10. func main() {
  11. flag.Parse()
  12. fmt.Printf("%s:%d", ip, port)
  13. }

Answer

  1. package main
  2. import "flag"
  3. import "fmt"
  4. var ip string
  5. var port int
  6. func init() {
  7. flag.StringVar(&ip, "ip", "0.0.0.0", "ip address")
  8. flag.IntVar(&port, "port", 8000, "port number")
  9. }
  10. func main() {
  11. flag.Parse()
  12. fmt.Printf("%s:%d", ip, port)
  13. }