使go变量接受多种类型

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

Make go variable accept multiple types

问题

我想在Go语言中开发一个库,利用Go标准库中的debug包中的不同文件格式(http://golang.org/pkg/debug/)。我的想法是打开一个文件并打印出有关该文件的信息。
现在,我想通过测试所有相关的文件类型来自动识别正确的文件格式。例如,为了测试一个文件是简单的Mach-O文件还是fat Mach-O文件,我尝试使用两种Open方法打开文件:

file, err := macho.Open(filename)
if err != nil {
    fmt.Println("不是Mach-O文件。")
}
file, err = macho.OpenFat(filename)
if err != nil {
    fmt.Println("不是fat Mach-O文件。退出。")
    return
}

不幸的是,文件变量(当然)经过类型检查,我得到以下错误:

在多重赋值中,无法将*macho.FatFile分配给file(类型为*macho.File)

我甚至不确定这是否是正确的方法。有人能指导我如何正确执行这个操作吗?

英文:

I would like to develop a library in Go which makes use of the different file formats of the debug package in the go standard packages (http://golang.org/pkg/debug/). The idea is to open a file and print out information about that file.
Now I would like to automatically recognize the correct file format by testing all relevant file types. For instance, to test if a file is a simple Mach-O or a fat Mach-O file, I am trying to open the file with both Open methods:

  file, err := macho.Open(filename)
  if err != nil {
      fmt.Println("Not an Mach-O file.")
  }
  file, err = macho.OpenFat(filename)
  if err != nil {
      fmt.Println("Not an fat Mach-O file. Exiting.")
      return
  } 

Unfortunately the file variable is (of course) type checked, and I get the following error:

cannot assign *macho.FatFile to file (type *macho.File) in multiple assignment

I am not even sure of this is the correct way of approaching this. Can anyone point me to the right direction how to perfom this properly?

答案1

得分: 2

或者根本不声明文件,如果你不打算对其进行任何操作。

如果你真的想在File上调用一些函数,那么你可以将其声明为一个带有你想要调用的函数的接口类型,然后你可以这样说:

var file io.Closer
file, err := macho.Open(filename)
if err != nil {
    fmt.Println("不是 Mach-O 文件。")
}
file, err = macho.OpenFat(filename)
if err != nil {
    fmt.Println("不是 fat Mach-O 文件。退出。")
    return
}
file.Close()

在这种情况下,这并不是很有趣,因为macho.File和macho.FatFile的唯一共同函数是Close()。

英文:

Or don't even declare file at all, if you're not going to do anything with it.

if _, err := macho.Open(filename); err != nil {
    fmt.Println("Not an Mach-O file.")
}
if _, err = macho.OpenFat(filename); err != nil {
    fmt.Println("Not an fat Mach-O file. Exiting.")
    return
} 

If you actually wanted to call some function on File, then you could declare it of an interface type with the func(s) you want to call, then you could say something like:

var file io.Closer
file, err := macho.Open(filename)
if err != nil {
    fmt.Println("Not an Mach-O file.")
}
file, err = macho.OpenFat(filename)
if err != nil {
    fmt.Println("Not an fat Mach-O file. Exiting.")
    return
}
file.Close()

In this case it't not that interesting because it looks like macho.File and macho.FatFile's only common function is Close()

答案2

得分: 1

由于Go语言是强类型的,你不能将不同类型的值赋给同一个变量。

解决这个问题的简单方法是在第二次调用时使用另一个变量。

file2, err := macho.OpenFat(filename)
if err != nil {
    fmt.Println("Not an fat Mach-O file. Exiting.")
    return
}

你也可以让OpenFatOpen方法返回相同的类型或实现相同的接口。

这里有一个关于Go接口如何工作的解释

英文:

Since go is strongly typed you can't assign different types to the same variable.

The easy way to fix your issue is to just use another variable in your second call.

file2, err := macho.OpenFat(filename)
if err != nil {
    fmt.Println("Not an fat Mach-O file. Exiting.")
    return
}

You could also have both OpenFat and Open methods return the same type or implement the same interface.

Here is a good explanation of how go interfaces work.

huangapple
  • 本文由 发表于 2014年9月21日 00:07:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/25950701.html
匿名

发表评论

匿名网友

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

确定