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.
package main
import "flag"
import "fmt"
var ip string
var port int
func init() {
// A
// B
}
func main() {
flag.Parse()
fmt.Printf("%s:%d", ip, port)
}
Answer
package main
import "flag"
import "fmt"
var ip string
var port int
func init() {
flag.StringVar(&ip, "ip", "0.0.0.0", "ip address")
flag.IntVar(&port, "port", 8000, "port number")
}
func main() {
flag.Parse()
fmt.Printf("%s:%d", ip, port)
}