我希望将int切片作为构造函数的输入,并返回指向原始列表的指针,将其类型强制转换为我的外部定制类型(type IntList []int
).
我可以这样做:
type IntList []int
func NewIntListPtr(ints []int) *IntList {
x := IntList(ints)
return &x
}
但我不能这么做:
type IntList []int
func NewIntListPtr(ints []int) *IntList {
return &ints
}
// or this for that matter:
func NewIntListPtr(ints []int) *IntList {
return &(IntList(ints))
}
// or this
func NewIntListPtr(ints []int) *IntList {
return &IntList(*ints)
}
// or this
func NewIntListPtr(ints *[]int) *IntList {
return &(IntList(*ints))
}
有没有一句俏皮话来实现这一点?