Advertisement
teknoraver

go pointer arithmetics

Oct 11th, 2016
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.71 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "unsafe"
  6. )
  7.  
  8. func main() {
  9.     var b []byte = []byte{1, 2, 3, 4, 5, 6, 7, 8}
  10.     var s []uint16 = *(*[]uint16)(unsafe.Pointer(&b))
  11.     var i []uint32 = *(*[]uint32)(unsafe.Pointer(&b))
  12.     var l []uint64 = *(*[]uint64)(unsafe.Pointer(&b))
  13.  
  14.     fmt.Printf(" u8: [%02x %02x %02x %02x %02x %02x %02x %02x]\n",
  15.            b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7])
  16.     fmt.Printf("u16: [%04x %04x %04x %04x]\n",
  17.            s[0], s[1], s[2], s[3])
  18.     fmt.Printf("u32: [%08x %08x]\n", i[0], i[1])
  19.     fmt.Printf("u64: [%016x]\n", l[0])
  20. }
  21.  
  22. /*
  23.  * example run:
  24.  * $ go run go_ptr.go
  25.  *  u8: [01 02 03 04 05 06 07 08]
  26.  * u16: [0201 0403 0605 0807]
  27.  * u32: [04030201 08070605]
  28.  * u64: [0807060504030201]
  29.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement