This is a creation in Article, where the information may have evolved or changed.
Almost all of the code comes from:
http://jan.newmarch.name/go/
Thanks to the author
/* IP */package mainimport ("FMT" "NET" "OS") Func main () {if Len (OS. Args)! = 2 {fmt. fprintf (OS. Stderr, "Usage:%s ip-addr\n", OS. ARGS[0]) os. Exit (1)}name: = OS. ARGS[1]ADDR: = Net. PARSEIP (name) if addr = = Nil {fmt. Println ("Invalid address")} else {fmt. Println ("The address is", addr. String ())}os. Exit (0)}
The net package is used mainly.
Os. Args is a command-line argument
It's a slice slice of a string.
Net. Parseip () as implies meaning is converted to an IP address is actually the "192.168.1.1" such a string into the form of IP in the Go language, the Go language IP is []byte type, this thing a slice slice. This app may not be much use, but the following version may be useful:
/* IP */package mainimport ("FMT" "NET" "OS") Func main () {if Len (OS. Args)! = 2 {fmt. fprintf (OS. Stderr, "Usage:%s ip-addr\n", OS. ARGS[0]) os. Exit (1)}name: = OS. ARGS[1]//ADDR: = Net. PARSEIP (name) addr, _:=net. Lookuphost (name) if addr[0] = = "" {fmt. Println ("Invalid address")} else {fmt. Println ("The address is" +addr[0])}os. Exit (0)}
After compiling successfully: run./IP www.baidu.com
Getting results: The address is 123.125.114.144
Net. Lookuphost () View the IP address of the domain name, the official explanation is:Lookuphost looks up the given host using the local resolver. It returns an array of the that host ' s addresses.
Hey