Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to type convert a slice of type aliases

Tags:

go

package main

import (
    "fmt"
)

type alias int
type aliases []*alias

func main() {
    a1 := alias(1)
    t := aliases{&a1}

    fmt.Println([]*int([]*alias(t)))
}

The type type aliases []*alias is essentially []*int

I want to be able to type convert aliases back to []*int

like image 633
samol Avatar asked Oct 22 '25 12:10

samol


1 Answers

You can with unsafe.Pointer, a little bit unsafe so not recommended

PointerToSliceOfPointersToInt := (*([]*int))(unsafe.Pointer(&t))

try it works https://play.golang.org/p/6AWd1W_it3

like image 176
Uvelichitel Avatar answered Oct 25 '25 09:10

Uvelichitel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!