3

In the Go blog, this is how to print the map in order.

http://blog.golang.org/go-maps-in-action

      import "sort"

      var m map[int]string
      var keys []int
      for k := range m {
          keys = append(keys, k)
      }
      sort.Ints(keys)
      for _, k := range keys {
          fmt.Println("Key:", k, "Value:", m[k])
      }

but what if I have the string keys like var m map[string]string

I can't figure out how to print out the string in order(not sorted, in order of string creation in map container)

The example is at my playground http://play.golang.org/p/Tt_CyATTA3 as you can see, it keeps printing the jumbled strings, so I tried map integer values to map[string]string but I still could not figure out how to map each elements of map[string]string.

http://play.golang.org/p/WsluZ3o4qd

3
  • What do you mean with: in order of string creation in map container you mean by the position they have in accurate_99? Can you post a sample input and output? Commented Oct 1, 2013 at 22:39
  • 1
    Looking at your code ( play.golang.org/p/Tt_CyATTA3 ) and your previous question ( stackoverflow.com/questions/19121331/… ) it really seems you're trying to attack a problem that's too complicated and probably requires a more careful design and data structure. Why not ask a question what you're actually trying to achieve instead of asking many small specific question about your implementation (that's likely not going to be well designed) Commented Oct 1, 2013 at 23:22
  • 1
    You may be interested in this: nltk.org since it looks like you're trying to do natural language processing which isn't trivial at all. Commented Oct 1, 2013 at 23:23

1 Answer 1

4

Well, the blog mentions that iteration order is randomized:

"...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"

The solution is kind of trivial, you have a separate slice with the keys ordered as you need:

"...If you require a stable iteration order you must maintain a separate data structure that specifies that order."

So, to work as you expect, create an extra slice with the correct order and the iterate the result and print in that order.

order := []string{"i", "we", "he", ....}

func String(result map[string]string) string { 
   for _, v := range order { 
      if present in result print it, 
   }
   ... print all the Non-Defined at the end 
  return stringValue
}

See it running here: http://play.golang.org/p/GsDLXjJ0-E

Sign up to request clarification or add additional context in comments.

2 Comments

THanks, it works, but I just wonder if I really need map container. I did it because go does not support a specific function to find an element in an array which means I need to traverse it everytime I need to find it, so I chose map to locate a certain element. But this gets very complicated, so I might just use array.
If the array is small enough I think iterating over it will be a very fast operation. On the other hand, using a map is a little bit faster and if but you don't really need to print you can stick with your initial implementation. The problem comes when you iterate the map anyway ( every time you print it ). Do you really need to keep them in order?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.