title: A safe way to add a key to map in Golang
body: <p>Due to this fact:</p>
  
  <blockquote>
    <p>If a map entry is created during iteration, that entry may be produced
    during the iteration or may be skipped. The choice may vary for each
    entry created and from one iteration to the next.</p>
  </blockquote>
  
  <p>It's not safe to add key-values to map during iteration:</p>
  
  <pre><code>var m = make(map[string]int)
  m["1"] = 1
  m["2"] = 2
  m["3"] = 3
  
  for k, v := range m {
      if strings.EqualFold( "2", k){
          m["4"] = 4
      }
      fmt.Println(k, v)
  }
  </code></pre>
  
  <p>Sometimes <code>"4"</code> key is produced, sometimes not. </p>
  
  <p>What is the workaround to make it always produced?</p>