【发布时间】:2019-04-13 06:57:02
【问题描述】:
我制作了一个应用来反转切片/数组。 用我自己的类型制作切片时遇到问题。 这是行不通的
type mytype int
func main() {
// []mytype doesn't work
// var slice = []mytype{11, 22, 33, 44}
但如果我将 mytype 更改为 int 它将起作用
// []int It works
var slice = []int{11, 22, 33, 44}
错误说
panic: interface conversion: interface {} is *[]main.mytype, not *[]int
我发现错误来自这一行
sliceType := *slice.(*[]int)
因为我将sliceType 声明为int。我仍然不知道如何在 Golang 中制作动态类型。
任何帮助将不胜感激。这是我的代码的 Golang Playground https://play.golang.org/p/5QyTMcZFGPi
【问题讨论】:
-
我收到错误
panic: reflect.Set: value of type []interface {} is not assignable to type []main.mytype -
“不知道如何在 Golang 中创建动态类型” - 你不能,句号。 Go 是一种静态类型语言。