使go变量接受多种类型

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?

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()

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.