File: unsafe3.go

package info (click to toggle)
golang-github-traefik-yaegi 0.16.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 24,608 kB
  • sloc: sh: 457; makefile: 39
file content (34 lines) | stat: -rw-r--r-- 425 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package main

import (
	"math/bits"
	"unsafe"
)

const (
	SSize = 16
	WSize = bits.UintSize / 8
)

type S struct {
	X int
	Y int
}

func main() {
	bigEndian := (*(*[2]uint8)(unsafe.Pointer(&[]uint16{1}[0])))[0] == 0
	var sBuf [SSize]byte
	s := (*S)(unsafe.Pointer(&sBuf[0]))

	s.X = 2
	s.Y = 4

	if bigEndian {
		println(sBuf[0+WSize-1], sBuf[WSize+WSize-1])
	} else {
		println(sBuf[0], sBuf[WSize])
	}
}

// Output:
// 2 4