Search code examples
gocompiler-construction

Where is the defer statement implemented?


I would like to know how and why the defer-Statement works and where it is implemented(in Compiler Source Code).

I found the package about parsing the defer-Statement and building it in the syntax-tree

But I'm interestetd in the actual exection of defer at runtime. My guess is, that it's somewhere inside the "src/runtime"


Solution

  • Begin reading here https://github.com/golang/go/blob/d089a6c7187f1ff85277515405ec6c641588a7ff/src/runtime/panic.go#L70

    You can also search the repository for the term "deferreturn" to find more results.

    Assembly for defer jumps here https://github.com/golang/go/blob/master/src/runtime/asm_amd64.s#L550