为什么以下Go代码有效?
也就是说,由于类型的f和b是interface{}(而不是任何bool,int或string),怎么可能对我来说,与脱身不是铸造或类型的断言f,并b在三个的if声明?
package main
import (
"fmt"
"reflect"
)
func foop(p map[string]interface{}) {
p["foo"] = true
}
func barp(p map[string]interface{}) {
p["bar"] = 17
}
func foop2(p map[string]interface{}) {
p["foo"] = "blah"
}
func main() {
p := make(map[string]interface{})
fmt.Printf("%v\n", p)
foop(p)
barp(p)
f := p["foo"]
b := p["bar"]
fmt.Printf("f: %T\n", f)
if f == true {
fmt.Println("ok")
}
fmt.Printf("b: %T\n", b)
if b == 17 {
fmt.Println("ok")
}
foop2(p)
f = p["foo"]
if f == "blah" {
fmt.Println("ok")
}
fmt.Printf("f: %T\n", f)
fmt.Printf("f: %s\n", reflect.TypeOf(f))
}
Run Code Online (Sandbox Code Playgroud)