mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2025-08-15 19:12:35 +00:00
good good study, day day up!
This commit is contained in:
38
vendor/gopl.io/ch13/unsafeptr/main.go
generated
vendored
Normal file
38
vendor/gopl.io/ch13/unsafeptr/main.go
generated
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
|
||||
// See page 357.
|
||||
|
||||
// Package unsafeptr demonstrates basic use of unsafe.Pointer.
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
func main() {
|
||||
//!+main
|
||||
var x struct {
|
||||
a bool
|
||||
b int16
|
||||
c []int
|
||||
}
|
||||
|
||||
// equivalent to pb := &x.b
|
||||
pb := (*int16)(unsafe.Pointer(
|
||||
uintptr(unsafe.Pointer(&x)) + unsafe.Offsetof(x.b)))
|
||||
*pb = 42
|
||||
|
||||
fmt.Println(x.b) // "42"
|
||||
//!-main
|
||||
}
|
||||
|
||||
/*
|
||||
//!+wrong
|
||||
// NOTE: subtly incorrect!
|
||||
tmp := uintptr(unsafe.Pointer(&x)) + unsafe.Offsetof(x.b)
|
||||
pb := (*int16)(unsafe.Pointer(tmp))
|
||||
*pb = 42
|
||||
//!-wrong
|
||||
*/
|
Reference in New Issue
Block a user