Skip to content

Instantly share code, notes, and snippets.

@yankooo
Last active September 1, 2020 10:28
Show Gist options
  • Save yankooo/56f5048fb27afabb17a605dcd3a59396 to your computer and use it in GitHub Desktop.
Save yankooo/56f5048fb27afabb17a605dcd3a59396 to your computer and use it in GitHub Desktop.
string2bytes
func BytesToStringWithNoCopy(bytes []byte) string {
return *(*string)(unsafe.Pointer(&bytes))
}
func StringToBytesWithNoCopy(s string) []byte {
x := (*[2]uintptr)(unsafe.Pointer(&s))
h := [3]uintptr{x[0], x[1], x[1]}
return *(*[]byte)(unsafe.Pointer(&h))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment