Anonymous user
Host introspection: Difference between revisions
→{{header|Go}}: Replace /proc hack
(Add Nimrod) |
(→{{header|Go}}: Replace /proc hack) |
||
Line 260:
import (
"runtime"
"strconv"
)
func main() {
fmt.Println(runtime.Version(), runtime.GOOS, runtime.GOARCH)
// inspect an int variable to determine endianness▼
if *(*byte)(unsafe.Pointer(&x)) == 1 {▼
x := uint32(0x01020304)
fmt.Println("little endian")▼
case 0x01:
case 0x04:
default:
}
// There are several ways of determining the size of an int/uint.
fmt.Println(" strconv.IntSize =", strconv.IntSize)
// That uses the following definition which can also be done by hand
intSize := 32 << uint(^uint(0)>>63)
fmt.Println("32 << uint(^uint(0)>>63) =", intSize)
// With Go 1.0, 64-bit architectures had 32-bit int and 64-bit
// uintptr. This was changed in Go 1.1. In general it would
// still be possible that int and uintptr (the type large enough
// to hold the bit pattern of any pointer) are of different sizes.
const bitsPerByte = 8
fmt.Println(" sizeof(int) in bits:", unsafe.Sizeof(int(0))*bitsPerByte)
fmt.Println(" sizeof(uintptr) in bits:", unsafe.Sizeof(uintptr(0))*bitsPerByte)
// If we really want to know the architecture size and not the size of int
// it safest to take the max of those.
archSize := unsafe.Sizeof(int(0))
if psize := unsafe.Sizeof(uintptr(0)); psize > archSize {
archSize = psize
}
fmt.Println(" architecture word size:", archSize*bitsPerByte)
}</lang>
{{out}}
<pre>
go1.3.1 freebsd amd64
little endian
strconv.IntSize = 64
32 << uint(^uint(0)>>63) = 64
sizeof(int) in bits: 64
sizeof(uintptr) in bits: 64
architecture word size: 64
</pre>
<pre>
go1.3.1 freebsd 386
little endian
strconv.IntSize = 32
32 << uint(^uint(0)>>63) = 32
sizeof(int) in bits: 32
sizeof(uintptr) in bits: 32
architecture word size: 32
</pre>
<pre>
go1.3.1 nacl amd64p32
little endian
strconv.IntSize = 32
32 << uint(^uint(0)>>63) = 32
sizeof(int) in bits: 32
sizeof(uintptr) in bits: 32
architecture word size: 32
</pre>
Alternative technique:
Line 318 ⟶ 347:
f.Close()
}</lang>
{{out}}
<pre>
LittleEndian
|