我是Go的新手,所以这可能是显而易见的.编译器不允许以下代码:(http://play.golang.org/p/3sTLguUG3l)
package main
import "fmt"
type Card string
type Hand []Card
func NewHand(cards []Card) Hand {
hand := Hand(cards)
return hand
}
func main() {
value := []string{"a", "b", "c"}
firstHand := NewHand(value)
fmt.Println(firstHand)
}
Run Code Online (Sandbox Code Playgroud)
错误是:
/tmp/sandbox089372356/main.go:15: cannot use value (type []string) as type []Card in argument to NewHand
从规范来看,它看起来像[]字符串与[]卡的底层类型不同,因此不能进行类型转换.
确实是这样,还是我错过了什么?
如果是这样的话,为什么会这样呢?假设,在一个非宠物示例程序中,我输入一个字符串片段,有没有办法将它"转换"成一片卡片,或者我是否必须创建一个新结构并将数据复制到其中?(我想避免使用,因为我需要调用的函数将修改切片内容).