casting a cgo array into a slice

后端 未结 1 1638
梦如初夏
梦如初夏 2020-12-11 14:09

At the moment I do this for casting a CGO array of doubles into a slice of float64:

doubleSlc := [6]C.double{}

// Fill doubleSlc

floatSlc := []float64{floa         


        
相关标签:
1条回答
  • 2020-12-11 14:38

    You have the normal and safe way of doing this:

    c := [6]C.double{ 1, 2, 3, 4, 5, 6 }
    fs := make([]float64, len(c))
    for i := range c {
            fs[i] = float64(c[i])
    }
    

    Or you could cheat unportably and do this:

    c := [6]C.double{ 1, 2, 3, 4, 5, 6 }
    cfa := (*[6]float64)(unsafe.Pointer(&c))
    cfs := cfa[:]
    

    If C.double and float64 are the same underlying type we can take a pointer to the C.double array, unsafely cast it to a pointer to a same size float64 array, then take a slice of that array.

    Of course it's called unsafe for a very good reason.

    0 讨论(0)
提交回复
热议问题