小编Tom*_*m L的帖子

是否可以在不使用界面的情况下使其工作?

为什么添加 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: …
Run Code Online (Sandbox Code Playgroud)

types interface go

-1
推荐指数
1
解决办法
109
查看次数

标签 统计

go ×1

interface ×1

types ×1