package main
import "fmt";
func Tern(exp bool, a interface{}, b interface{}) (interface{}) {
if exp { return a }
return b
}
func main() {
a := 7; b := 1;
result := Tern(a > b, a, b);
fmt.Printf("%d\n", result);
}
interface{}
是什么意思? - user181548reflect
的一些库代码,但最终有点困惑。它在Printf
例程中经常使用,其中变量的类型是从变量本身获取的。但我在一个错综复杂的依赖迷宫中迷失了方向。 - user181548这篇名为“在Go中的接口 - 第二部分:帮助适应性、进化式设计”(2012年1月,来自Sathish VJ)的教程清楚地提到了Go接口的主要优势:
Go的接口不是Java或C#接口的变体,它们更加强大。
它们是大规模编程和适应性、进化式设计的关键。
请看同一篇文章中有关巴士不同视角(接口)的示例:
package main
import "fmt"
//Go Step 1: Define your data structures
type Bus struct {
l, b, h int
rows, seatsPerRow int
}
//Go Step 2: Define a real world abstraction that could use the data we structure we have
type Cuboider interface {
CubicVolume() int
}
//Go Step 3: Implement methods to work on data
func (bus Bus) CubicVolume() int {
return bus.l * bus.b * bus.h
}
//Go step - repeat 2 & 3 for any other interfaces
type PublicTransporter interface {
PassengerCapacity() int
}
func (bus Bus) PassengerCapacity() int {
return bus.rows * bus.seatsPerRow
}
func main() {
b := Bus{
l:10, b:6, h:3,
rows:10, seatsPerRow:5}
fmt.Println("Cubic volume of bus:", b.CubicVolume())
fmt.Println("Maximum number of passengers:", b.PassengerCapacity())
}
PersonalSpaceLaw
的新接口,它不同于它已经实现的任何其他接口。//new requirement that the Bus must be compatible with
type PersonalSpaceLaw interface {
IsCompliantWithLaw() bool
}
func (b Bus) IsCompliantWithLaw() bool {
return (b.l * b.b * b.h) / (b.rows * b.seatsPerRow) >= 3
}
package main
type Stringer interface {
String() string
}
type pie int
type pizza string
func (p pie) String() string{
return "pie"
}
func (p pizza) String() string{
return "pizza"
}
func main(){
var a pie
var b pizza
fmt.Println(a,b) //fmt.Println() will look for Stringers and call their String() method.
}
package main
import (
"encoding/json"
. "fmt"
)
func main() {
var i interface{} = c
e := func() error { return c } // type error interface { Error() string}
Println(e()) // Hiss
s := func() Stringer { return d } // type Stringer interface {String() string}
// func Println(a ...interface{}) (n int, err error)
Println(s()) // Woof
d := `{"Pet":"Dog","Age":2, "Eat": "Bone"}`
json.Unmarshal([]byte(d), &i) // func Unmarshal(data []byte, v interface{}) error
m := i.(map[string]interface{})
Println(m["Age"]) // 2
}
type cat string
type dog string
var c cat
var d dog
func (cat) Error() string { return "Hiss" }
func (dog) String() string { return "Woof" }
func() error { return c }
调用 type error interface { Error() string}
。 func (cat) Error() string
实现了type error interface { Error() string}
。请参见https://blog.golang.org/error-handling-and-go。fmt.Println
调用String方法。参见https://talks.golang.org/2013/go4python.slide#33。func() Stringer { return d }
调用type Stringer interface {String() string}
。func (dog) String() string
实现了type Stringer interface {String() string}
。
fmt.Println
的签名是func Println(format string, a ...interface{}) (n int, err error)
,这意味着它的参数(在格式字符串之后)是接口值。参见https://blog.golang.org/constants。引号已编辑以匹配示例。
标准库中广泛使用的关键接口使得将API链接在一起变得容易。请参见https://talks.golang.org/2012/goforc.slide#46。更多示例请参见https://talks.golang.org/2014/go4gophers和https://talks.golang.org/2014/go4gophers.slide#1。
维基百科解释了鸭子类型,并在Go语言中提供了一个示例。 http://en.wikipedia.org/wiki/Duck_typing