title: Initialization loop Golang
body: <p>Hi I'm trying to make my function unit testable. One of the suggestion was to assign the function into a variable and make it globally accessible. I just did that but now I'm experiencing a Initialization loop below is my code</p>
  
  <pre><code>////////////////////////
  // main.go
  ////////////////////////
  
  func DownloadFile(filename string) {
      // Initialized request variable here
      // ... doing initialization
      // End of initialization
  
      res = ProcessDownload(request)
  
      if res == 401 {
         return doRetry(filename)
      }
  
      // some code here
      return downloadFile(filename)
  }
  
  func DoRetry(filename string) {
      // Doing some database insert/updating
     return downloadFile(string)
  }
  </code></pre>
  
  <p>And in my other file I assign this function into variable</p>
  
  <pre><code>//////////////////////
  // global.go
  //////////////////////
  
  var downloadFile = DownloadFile
  var doRetry = DoRetry
  </code></pre>
  
  <p>The reason I did this in the global.go was to make the <strong>DoRetry</strong> and <strong>DownloadFile</strong> unit testable. Meaning with this method I can <strong>MOCK</strong> the function without creating an interface. Since this is just a standalone function and don't need to be within a certain class. All the rest of the variable is ok but when it comes to a function doing a recursive behavior I get the error below</p>
  
  <pre><code>./global.go:22:5: initialization loop:
  /go/src/project/global.go:22:5 downloadFile refers to
  /go/src/project/process.go:403:99 DownloadFile refers to
  /go/src/project/global.go:23:5 doRetry refers to
  /go/src/project/process.go:394:89 DoRetry refers to
  /go/src/project/global.go:22:5 downloadFile
  FAIL    project [build failed]
  </code></pre>
  
  <p>I know this maybe a duplicate in <a href="https://stackoverflow.com/questions/31726254/how-to-avoid-initialization-loop-in-go">here</a> but I can't see how the solution in here solve my problem.</p>
  
  <p>Please help me on this. Thanks
  I read about mocking a function in this link</p>
  
  <p><a href="https://stackoverflow.com/questions/19167970/mock-functions-in-go">Mock functions in Go</a></p>
  
  <p><a href="https://husobee.github.io/golang/testing/unit-test/2015/06/08/golang-unit-testing.html" rel="nofollow noreferrer">https://husobee.github.io/golang/testing/unit-test/2015/06/08/golang-unit-testing.html</a></p>