如何定义一个空变量来存储File结构的值?

huangapple go评论154阅读模式
英文:

How do you define an empty variable to store the value of a File struct?

问题

我正在尝试找到一种声明空变量以存储os.Createos.Open的返回值的方法。代码如下:

func main() {

  var path = "list.txt"
  // 一些尝试:
  // var file File{}
  // var file *File

  // 检查文件是否存在
  var _, err = os.Stat(path)

  // 如果文件不存在,创建文件
  if os.IsNotExist(err) {
    var file, err = os.Create(path)

    // 如果文件存在,打开文件
  } else {
    var file, err = os.Open(path)
  }

  // 检查打开和创建文件时的错误
  if err != nil {
    log.Fatal(err)
    return
  }

  // 延迟关闭文件
  defer file.Close()

}

这两个尝试都导致以下错误:

./main.go:13: undefined: File

我确定这是我不知道自己不知道的事情之一。我知道的是:

  • 根据os/file.go,我要找的返回值类型是*File
  • 该类型在os/file_unix.go中被定义为一个结构体

有人能解释一下:

  1. 我如何创建一个空变量,然后用它来存储os.Createos.Open结果中的第一个变量?
  2. 为什么我的两个尝试是错误的?
  3. 我还有什么误解吗?
英文:

I am trying to figure out a way to declare an empty variable to store the return values of os.Create and os.Open. 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()

}

Both attempts result in the following error:

> ./main.go:13: undefined: File

I'm sure this is one of those things that I don't know I don't know. What I do know:

  • Per os/file.go, type of the return value I'm looking for is *File
  • That type is defined in os/file_unix.go as a struct

Can someone explain to me:

  1. How do I create an empty variable that can then be used to store the first variable in the results of os.Create and os.Open.
  2. Why were my two attempts wrong?
  3. Anything else that I'm misunderstanding.

答案1

得分: 5

定义变量的语法是 var <变量名> <类型>,了解更多关于变量的信息请参考这里

var file *os.File
var err error

你更新后的代码:

func main() {
  path := "list.txt"
  
  var file *os.File
  var err error

  // 检查文件是否存在
  if err = os.Stat(path); os.IsNotExist(err) {
    file, err = os.Create(path)
  } else { // 如果文件存在,则打开它
    file, err = os.Open(path)
  }

  // 检查打开和创建文件时是否出错
  if err != nil {
    log.Fatal(err)
    return
  }

  // 延迟关闭文件
  defer file.Close()
}
英文:

Defining variable is var &lt;variable-name&gt; &lt;type&gt;, learn more about variables.

var file *os.File
var err error

Your updated code:

func main() {
  path := &quot;list.txt&quot;
  
  var file *os.File
  var err error

  // Check if file exists
  if err = os.Stat(path); os.IsNotExist(err) {
    file, err = os.Create(path)
  } else { // If file exists, open it
    file, err = os.Open(path)
  }

  // Check errors opening and creating file
  if err != nil {
    log.Fatal(err)
    return
  }

  // Defer closing file
  defer file.Close()
}

huangapple
  • 本文由 发表于 2017年7月10日 05:31:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/45001221.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定