title: How to test file.Open with os.O_RDWR and os.O_CREATE flags?
body: <p>I'm trying to write unit test for struct constructor, which may return also nil if error happens during file.Open. I don't have idea how to test/mock file error with flags: os.O_RDWR|os.O_CREATE|os.O_APPEND</p>
  
  <p>I tried to check nil value inside test, but it failed.</p>
  
  <p>Constructor:</p>
  
  <pre class="lang-golang prettyprint-override"><code>type App struct {
      someField string
      log *log.Logger
  }
  
  func New() *App {
      app := &amp;App{}
      f, err := os.OpenFile("info.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
      if err != nil {
          fmt.Printf("error opening file: %v", err)
          return nil
      }
      mw := io.MultiWriter(os.Stdout, f)
      l = log.New(mw, "APP", log.Ldate|log.LstdFlags|log.Lshortfile)
      app.log = l
  
      return app
  }
  </code></pre>
  
  <p>And test for constructor:</p>
  
  <pre class="lang-golang prettyprint-override"><code>func TestNew(t *testing.T) {
      var a App
      a = New()
  
      // doesn't cover
      if a == nil {
          t.Fatal("Error opening file")
      }
  }
  </code></pre>
  
  <p>I expect to have covered error != nil, which in coverage is red:</p>
  
  <pre class="lang-golang prettyprint-override"><code>f, err := os.OpenFile("info.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
  if err != nil {
    fmt.Printf("error opening file: %v", err)
    return nil
  }
  </code></pre>