dongli2000 2018-05-17 08:00
浏览 154
已采纳

如何在go中将切片转换为别名切片? [关闭]

I alias int to Int.

I want to convert a slice of Int to a slice of int, but got a compile error:

cannot convert c (type []Int) to type []int How can I fix this? Thank you.

package main

import (
    "fmt"
)

type Int int

func main() {
    var c = []Int{}
    var x = []int( c )
    fmt.Println(len(x))
}
  • 写回答

4条回答 默认 最新

  • dongliping003116 2018-05-17 08:20
    关注

    Your Int type is not an alias of int, it's a new type with int being its underlying type. This type of conversion is not supported / allowed by the language spec. More specifically, converting a slice type to another where the element type is different is not allowed.

    The safe way

    If you only need an []int "view" of the []Int, the safe way to "convert" would be to create a copy of the []Int slice but with a type of []int, and use a for range loop and convert each individual element from Int to int type:

    var c = []Int{1, 2}
    
    x := make([]int, len(c))
    for i, v := range c {
        x[i] = int(v)
    }
    fmt.Println(x)
    

    Output (try it on the Go Playground):

    [1 2]
    

    The unsafe way

    There is also an "unsafe" way:

    var c = []Int{1, 2}
    
    var x []int = *(*[]int)(unsafe.Pointer(&c))
    fmt.Println(x)
    

    Output is the same. Try this one on the Go Playground.

    What happens here is that the address of c (which is &c) is converted to unsafe.Pointer (all pointers can be converted to this), which then is converted to *[]int (unsafe.Pointer can be converted to any pointer type), and then this pointer is dereferenced which gives a value of type []int. In this case it is safe because the memory layout of []Int and []int is identical (because Int has int as its underlying type), but in general, use of package unsafe should be avoided whenever possible.

    If Int would be a "true" alias

    Note that if Int would be a "true" alias to int, the conversion would not even be needed:

    var c = []Int{1, 2}
    
    var x []int = c
    fmt.Println(x)
    

    Output is the same as above (try it on the Go Playground). The reason why this works is because writing []Int is identical to writing []int, they are the same type, so you don't even need a conversion here.

    By using a slice type

    Also note that if you would create a new type with []int as its underlying type, you could use type conversion:

    type IntSlice = []int
    
    func main() {
        var c = IntSlice{1, 2}
    
        var x []int = []int(c)
        fmt.Println(x)
    }
    

    Output is again the same. Try this one on the Go Playground.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题