du16178
2017-12-29 04:31
浏览 211
已采纳

如何在Golang中将自定义类型切片转换为原始切片?

type TCustomIntType int

func aFunc() {
    var fails []TCustomIntType = []TCustomIntType([]int{})
}

I got:

cannot convert []int literal (type []int) to type []TCustomIntType

How to fix it? Do I have to write a convert func manually?

图片转代码服务由CSDN问答提供 功能建议

  type TCustomIntType int 
 
func aFunc(){
 var failed [] TCustomIntType = []  TCustomIntType([[int {})
} 
   
 
 

我得到了:

无法转换[] int 文字(类型[] int)键入[] TCustomIntType

如何解决? 我必须手动编写转换函数吗?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doujia5863 2017-12-29 04:52
    已采纳

    Yes, you have to copy it manually with a for loop.

    点赞 打赏 评论

相关推荐 更多相似问题