Created
June 4, 2020 06:54
-
-
Save coyove/9ea3b2cc33e13fce89da601ff22c0384 to your computer and use it in GitHub Desktop.
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
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