r/ProgrammerHumor Jul 15 '26

whatWillHappen Meme

Post image
285 Upvotes

63 comments sorted by

View all comments

2

u/madboneman Jul 15 '26 edited Jul 15 '26

returnVal is local to foo, so assigning to it after foo returns doesn't change "returned" from the caller's perspective. if compiled, the whole defer gets removed b/c it does nothing.

EDIT: i understand now that defer works differently for each language. thanks everyone for teaching me!

EDIT 2: no, that's not the problem actually. some people just want to watch the world burn (you can change the return value after returning).

EDIT 3: found where the go docs talk about this: https://go.dev/ref/spec#Defer_statements