title: How do you define an empty variable to store the value of a File struct?
body: <p>I am trying to figure out a way to declare an empty variable to store the return  values of <code>os.Create</code> and <code>os.Open</code>. Code:</p>
  
  <pre><code>func main() {
  
    var path = "list.txt"
    // Some attempts:
    // var file File{}
    // var file *File
  
    // Check if file exists
    var _, err = os.Stat(path)
  
    // If new file, create it
    if os.IsNotExist(err) {
      var file, err = os.Create(path)
  
      // If file exists, open it
    } else {
      var file, err = os.Open(path)
    }
  
    // Check errors opening and creating file
    if err != nil {
      log.Fatal(err)
      return
    }
  
    // Defer closing file
    defer file.Close()
  
  }
  </code></pre>
  
  <p>Both attempts result in the following error:</p>
  
  <blockquote>
    <p>./main.go:13: undefined: File</p>
  </blockquote>
  
  <p>I'm sure this is one of those things that I don't know I don't know. What I do know:</p>
  
  <ul>
  <li>Per <code>os/file.go</code>, type of the return value I'm looking for is <code>*File</code></li>
  <li>That type is defined in <code>os/file_unix.go</code> as a struct</li>
  </ul>
  
  <p>Can someone explain to me:</p>
  
  <ol>
  <li>How do I create an empty variable that can then be used to store the first variable in the results of <code>os.Create</code> and <code>os.Open</code>.</li>
  <li>Why were my two attempts wrong?</li>
  <li>Anything else that I'm misunderstanding.</li>
  </ol>