title: Dependency injection in Go
body: <p>I'm looking for an appropriate way to inject dependencies.</p>
  <p>Say I have this code where the FancyWrite and FancyRead functions have a dependency on the WriteToFile and ReadFromFile functions. Since these have side effects I'd like to be able to inject them so I can replace them in tests.</p>
  <pre><code>package main
  
  func main() {
      FancyWrite()
      FancyRead()
  }
  
  ////////////////
  
  func FancyWrite() {
      WriteToFile([]byte(&quot;content...&quot;))
  }
  
  func FancyRead() {
      ReadFromFile(&quot;/path/to/file&quot;)
  }
  
  ////////////////
  
  func WriteToFile(content []byte) (bool, error) {
      return true, nil
  }
  
  func ReadFromFile(file string) ([]byte, error) {
      return []byte{}, nil
  }
  </code></pre>
  <p>One thing I tried is just put them as parameters into the functions:</p>
  <pre><code>package main
  
  func main() {
      FancyWrite(WriteToFile)
      FancyRead(ReadFromFile)
  }
  
  ////////////////
  
  func FancyWrite(writeToFile func(content []byte) (bool, error)) {
      writeToFile([]byte(&quot;content...&quot;))
  }
  
  func FancyRead(readFromFile func(file string) ([]byte, error)) {
      readFromFile(&quot;/path/to/file&quot;)
  }
  
  ////////////////
  
  func WriteToFile(content []byte) (bool, error) {
      return true, nil
  }
  
  func ReadFromFile(file string) ([]byte, error) {
      return []byte{}, nil
  }
  </code></pre>
  <p>So, this actually works great, but I could see this becoming harder to maintain for more dependencies. I also tried a factory pattern like the following so that the main function doesn't have to concern itself with building the FancyWrite function. But, the syntax is getting a little hard to read and with even more functions would be hard to maintain.</p>
  <pre><code>func FancyWriteFactory(writeToFile func(content []byte) (bool, error)) func() {
      return func() {
          FancyWrite(writeToFile)
      }
  }
  </code></pre>
  <p>So next I tried housing the functions as methods in a struct:</p>
  <pre><code>package main
  
  func main() {
      dfu := DefaultFileUtil{}
      ffm := FancyFileModule{
          FileUtil: &amp;dfu,
      }
  
      ffm.FancyWrite()
      ffm.FancyRead()
  }
  
  ////////////////
  
  type FileUtil interface {
      WriteToFile(content []byte) (bool, error)
      ReadFromFile(file string) ([]byte, error)
  }
  
  type FancyFileModule struct {
      FileUtil
  }
  
  func (fm *FancyFileModule) FancyWrite() {
      fm.FileUtil.WriteToFile([]byte(&quot;content...&quot;))
  }
  
  func (fm *FancyFileModule) FancyRead() {
      fm.FileUtil.ReadFromFile(&quot;/path/to/file&quot;)
  }
  
  ////////////////
  
  type DefaultFileUtil struct{}
  
  func (fu *DefaultFileUtil) WriteToFile(content []byte) (bool, error) {
      return true, nil
  }
  
  func (fu *DefaultFileUtil) ReadFromFile(file string) ([]byte, error) {
      return []byte{}, nil
  }
  </code></pre>
  <p>Now, this actually works well and is cleaner. However, I'm worried I am just shoehorning my functions as methods now and something just felt odd about that. I guess I can reason about it because structs are good when you have some state, and I guess I can count the dependencies as state?</p>
  <p>Those are the things I tried. So my question is, what is the proper way to do dependency injection in this case when the only reason to put functions as methods is to make them be a collection of dependencies elsewhere?</p>
  <p>Thanks!</p>