Skip to content

Instantly share code, notes, and snippets.

@coyove
Created June 4, 2020 06:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coyove/9ea3b2cc33e13fce89da601ff22c0384 to your computer and use it in GitHub Desktop.
Save coyove/9ea3b2cc33e13fce89da601ff22c0384 to your computer and use it in GitHub Desktop.
type str struct {
p unsafe.Pointer
a int
}
func mstr(s string) str {
ss := *(*[2]uintptr)(unsafe.Pointer(&s))
fmt.Println(ss)
return str{
p: unsafe.Pointer(ss[0]),
a: int(ss[1]),
}
}
func (s str) String() string {
var ss string
v := (*[2]uintptr)(unsafe.Pointer(&ss))
(*v)[0] = uintptr(s.p)
(*v)[1] = uintptr(s.a)
return ss
}
func TestStr(t *testing.T) {
// a := "b"
var s []str
for i := 0; i < 3; i++ {
s = append(s, mstr("a"+strconv.Itoa(i)))
}
runtime.GC()
t.Log(s)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment