Created
September 24, 2020 02:53
-
-
Save cloverstd/ddbebc244f45cf7e81a26982af74ff27 to your computer and use it in GitHub Desktop.
access value via unsafe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func main() { | |
var i interface{} = 100 | |
/** | |
type eface struct { | |
_type *_type | |
data unsafe.Pointer | |
} | |
*/ | |
n := *(*int)(*(*unsafe.Pointer)(unsafe.Pointer(uintptr(unsafe.Pointer(&i)) + unsafe.Alignof(i)))) | |
fmt.Println(n) | |
type eface struct { | |
_type uintptr // placeholder | |
data unsafe.Pointer | |
} | |
n = *(*int)((*eface)(unsafe.Pointer(&i)).data) | |
fmt.Println(n) | |
type eface2 struct { | |
_type uintptr // placeholder | |
data *int | |
} | |
n = *((*eface2)(unsafe.Pointer(&i)).data) | |
fmt.Println(n) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment