在go语言程序中经常能看到下面这些定义
func errorf(linenum int, format string, args ...interface{})
上面的语句interface{}类型代表函数的最终参数可以是任何类型
items map[interface{}]*CacheItem
上面的语句interface{}标识map的key可以是任何类型。
下面针对这种写法我们看看《The Go Programming Language》第七章第三节是怎么描述的:
A type satisfies an interface if it possesses all the methods the interface requires. For example, an *os.File satisfies io.Reader, Writer, Closer, and ReadWriter. A *bytes.Buffer satisfies Reader, Writer, and ReadWriter, but does not satisfy Closer becaus e it does not have a Close method. As a shorthand, Go programmers often say that a concrete type “is a”’ particular interface type, meaning that it satisfies the interface. For example, a *bytes.Buffer is an io.Writer; an *os.File is an io.ReadWriter.
如果类型拥有接口所需的所有方法,则类型匹配接口。 例如,*os.File满足io.Reader,Writer,Closer和ReadWriter。 *bytes.Buffer满足Reader,Writer和ReadWriter,但不满足Closer,因为它没有Close方法。 作为简写,Go程序员经常说具体类型“是一种”特定的接口类型,这意味着它满足了接口。 例如,* bytes.Buffer是一个io.Writer; *os.File是一个io.ReadWriter。
The assignability rule for interfaces is very simple: an expression may be assigned to an interface only if its type satisfies the interface. So:
接口的可分配性规则非常简单:只有当接口的类型满足接口时,才能将表达式分配给接口。 所以:
var w io.Writer
w = os.Stdout // OK: *os.File has Write method
w = new(bytes.Buffer) // OK: *bytes.Buffer has Write method
w = time.Second // compile error: time.Duration lacks Write method
var rwc io.ReadWriteCloser
rwc = os.Stdout // OK: *os.File has Read, Write, Close methods
rwc = new(bytes.Buffer) // compile error: *bytes.Buffer lacks Close method
An interface with more methods, such as io.ReadWriter, tells us more about the values it contains, and places greater demands on the types that implement it, than does an interface with fewer methods such as io.Reader. So what does the type interface{}, which has no methods at all, tell us about the concrete types that satisf y it?
具有更多方法的接口,例如io.ReadWriter,告诉我们更多关于它包含的值,并且对实现它的类型提出了更大的要求,而不是像io.Reader这样的更少方法的接口。 那么没有方法的类型interface{}告诉我们满足它的具体类型是什么?
That’s right: nothing. This may seem useless, but in fac t the type interface{}, which is called the empty interface type, is indispens able. Because the empty interface type places no demands on the types that satisfy it, we can assign any value to the empty interface.
没错:没有任何类型。 这似乎没用,但是在类型interface {}中,它被称为空接口类型,是不可或缺的。 因为空接口类型对满足它的类型没有要求,所以我们可以为空接口分配任何值。
var any interface{}
any = true
any = 12.34
any = "hello"
any = map[string]int{"one": 1}
any = new(bytes.Buffer)
Of course, having created an interface{} value containing a boolean, float, string , map, pointer, or any other type, we can do nothing directly to the value it holds since the interface has no methods. We need a way to get the value back out again. We’ll see how to do that using a type assertion in Section 7.10.
当然,创建了一个包含布尔值,浮点数,字符串,映射,指针或任何其他类型的interface{}值,由于接口没有方法,我们无法直接处理它所拥有的值。 我们需要一种方法来重新获得值。 我们将在第7.10节中看到如何使用类型断言来做到这一点。
如何重新获得值呢,先看看下面的代码:
var a interface{}
a = "hello world"
var buff string
buff = "BarryLyndon" + a
上面的代码运行会报错:invalid operation: "BarryLyndon" + a (mismatched types string and interface {})
这是类型不匹配的错误,那么如何从新获取a中的实际值呢,将buff = "BarryLyndon" + a语句换成buff = "BarryLyndon" + a.(string)就可以了
本文深入探讨了Go语言中空接口(interface{}

1263

被折叠的 条评论
为什么被折叠?



