title: Static global variables in Go
body: <p>I am new in Go language and I need some help. I have declared a global variable but the problem is that it does not keep its value. It would be solved with a static variable but no such variables exist in Go. How should I solve this?</p>
  
  <p><code>gID</code> is a global variable. This function is called twice. On the first call <code>if</code> code is executed. On the second, <code>else</code> code is executed. I want the second struct <code>Learner</code> instance, to get first Learner's instance ID. </p>
  
  <p><strong>I have to mention that NewLearner is called twice from two different files from different packages</strong>.</p>
  
  <pre><code>func NewLearner(name string, peerURLs types.URLs, clusterName string, now *time.Time) *Learner {
  
      l := &amp;Learner{
          RaftAttributes: RaftAttributes{PeerURLs: peerURLs.StringSlice()},
          Attributes:     Attributes{Name: name},
      }
  
      var b []byte
      sort.Strings(l.PeerURLs)
      for _, p := range l.PeerURLs {
          b = append(b, []byte(p)...)
      }
  
      b = append(b, []byte(clusterName)...)
      if now != nil {
          b = append(b, []byte(fmt.Sprintf("%d", now.Unix()))...)
          hash := sha1.Sum(b)
          l.ID = types.ID(binary.BigEndian.Uint64(hash[:8]))
          gID=l.ID
          return l
      }  else {
          l.ID = gID
          return l    
      }
  
  }
  </code></pre>