Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "unsafe"
- )
- func main() {
- var b []byte = []byte{1, 2, 3, 4, 5, 6, 7, 8}
- var s []uint16 = *(*[]uint16)(unsafe.Pointer(&b))
- var i []uint32 = *(*[]uint32)(unsafe.Pointer(&b))
- var l []uint64 = *(*[]uint64)(unsafe.Pointer(&b))
- fmt.Printf(" u8: [%02x %02x %02x %02x %02x %02x %02x %02x]\n",
- b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7])
- fmt.Printf("u16: [%04x %04x %04x %04x]\n",
- s[0], s[1], s[2], s[3])
- fmt.Printf("u32: [%08x %08x]\n", i[0], i[1])
- fmt.Printf("u64: [%016x]\n", l[0])
- }
- /*
- * example run:
- * $ go run go_ptr.go
- * u8: [01 02 03 04 05 06 07 08]
- * u16: [0201 0403 0605 0807]
- * u32: [04030201 08070605]
- * u64: [0807060504030201]
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement