去穿筋运动的麻烦

For the Stringers exercise in tour of Go: I got two different outputs for two different format printings. And the only thing I changed was the format verbs. They were %v and %d. Theoretically, they would give the same output. However the output's order was changed too, which was so weird. Any ideas about that? Below is my code:

package main

import "fmt"

type IPAddr [4]byte

// TODO: Add a "String() string" method to IPAddr.
func (a IPAddr)String() string{
    // Here is what I changed
    return fmt.Sprintf("%d.%d.%d.%d",a[0],a[1],a[2],a[3])
}

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, a)
    }
}

OutPut:

googleDNS: 8.8.8.8

loopback: 127.0.0.1

package main

import "fmt"

type IPAddr [4]byte

// TODO: Add a "String() string" method to IPAddr.
func (a IPAddr)String() string{
    // Here is what I changed
    return fmt.Sprintf("%v.%v.%v.%v",a[0],a[1],a[2],a[3])
}

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, a)
    }
}

Output:

loopback: 127.0.0.1

googleDNS: 8.8.8.8

The output's order was also changed.

Maps are not ordered.

When iterating over a map with a range loop, the iteration order is not specified and is not guaranteed to be the same from one iteration to the next[1]

[1]https://blog.golang.org/go-maps-in-action#TOC_7.

I don't see any difference with %v and %d output other than order.

package main

import "fmt"

type IPAddr [4]byte

// TODO: Add a "String() string" method to IPAddr.

func (ip IPAddr) String() string {
   var s string
   for i:= range ip{
     if(i==0){
      s +=fmt.Sprint(int(ip[i]))
     } else{
        s +="."+fmt.Sprint(int(ip[i]))
     }
  }     

  return s
}
func main() {
  hosts := map[string]IPAddr{
    "loopback":  {127, 0, 0, 1},
    "googleDNS": {8, 8, 8, 8},
  }
  for name, ip := range hosts {
    fmt.Printf("%v: %v
", name, ip)
  }
}