Created
September 14, 2019 05:55
-
-
Save blanchonvincent/a247b6c2af559b62f93377b5d7581b7f to your computer and use it in GitHub Desktop.
Medium - Keep Alive
This file contains 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 File struct { d int } | |
func main() { | |
p := openFile("t.txt") | |
content := readFile(p.d) | |
println("Here is the content: "+content) | |
} | |
func openFile(path string) *File { | |
d, err := syscall.Open(path, syscall.O_RDONLY, 0) | |
if err != nil { | |
panic(err) | |
} | |
p := &File{d} | |
runtime.SetFinalizer(p, func(p *File) { | |
syscall.Close(p.d) | |
}) | |
return p | |
} | |
func readFile(descriptor int) string { | |
doSomeAllocation() | |
var buf [1000]byte | |
_, err := syscall.Read(descriptor, buf[:]) | |
if err != nil { | |
panic(err) | |
} | |
return string(buf[:]) | |
} | |
func doSomeAllocation() { | |
var a *int | |
// memory increase to force the GC | |
for i:= 0; i < 10000000; i++ { | |
i := 1 | |
a = &i | |
} | |
_ = a | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment