为什么添加 type 时以下内容有效,Pet []interface{Name()}但添加 type 时无效Pet []string?是否可以在不使用界面的情况下使其工作?
package main
import "fmt"
type Pet []string // cannot use Cat("Puss") (type Cat) as type string in array or slice literal
// type Pet []interface{Name()} // prt Fluffy
type Cat string
func (c Cat) Name() {
fmt.Println(c)
}
func main() {
p := Pet{Cat("Whiskers"), Cat("Fluffy")}
p1 := p[1]
p1.Name()
}
./oo3.go:15:14: cannot use Cat("Whiskers") (type Cat) as type string in array or slice literal
./oo3.go:15:31: cannot use Cat("Fluffy") (type Cat) as type string in array or slice literal
./oo3.go:17:4: p1.Name undefined (type string has no field or method Name)
翻翻过去那场雪
相关分类