Skip to content

Instantly share code, notes, and snippets.

@cloverstd
Created September 24, 2020 02:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cloverstd/ddbebc244f45cf7e81a26982af74ff27 to your computer and use it in GitHub Desktop.
Save cloverstd/ddbebc244f45cf7e81a26982af74ff27 to your computer and use it in GitHub Desktop.
access value via unsafe
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