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条)

报告相同问题?

悬赏问题

  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型