import "fmt"
type IPAddr [4]byte
// TODO: Add a "String() string" method to IPAddr.
func (ip IPAddr) String() string {
var ls_ip string
for i, value := range ip {
//ls_ip = ls_ip + string(i) + ":" + string(int(value)) + "."
switch {
case i == 0:
ls_ip = fmt.Sprintf("%v", value)
default:
ls_ip = fmt.Sprintf("%v.%v", ls_ip, value)
}
}
return ls_ip
}
func main() {
addrs := map[string]IPAddr{
"loopback": {127, 0, 0, 1},
"googleDNS": {8, 8, 8, 8},
}
for n, a := range addrs {
fmt.Printf("%v: %v\n", n, a)
}
}
Output:
loopback: 127.0.0.1 googleDNS: 8.8.8.8 Program exited.
Reference:
http://tour.golang.org/methods/7
No comments:
Post a Comment