LoginSignup
0
0

More than 5 years have passed since last update.

むりやり型変換

Posted at

何らかの事情で全く異なる型の間の変換をする方法です。

unsafe です。

package main

import (
    "fmt"
    "unsafe"
)

func main() {
    aa := []bool{true, false, true}

    //bb := ([]int8)(aa)
    //cannot convert aa (type []bool) to type []int8

    bb := *(*[]int8)(unsafe.Pointer(&aa))

    fmt.Printf("aa=%#v, bb=%#v\n", aa, bb) //aa=[]bool{true, false, true}, bb=[]int8{1, 0, 1}
}

普通は使うものではありませんが、参考までに。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0