"Go defer"의 두 판 사이의 차이

 
(다른 사용자 한 명의 중간 판 하나는 보이지 않습니다)
5번째 줄: 5번째 줄:
The deferred call's arguments are evaluated immediately, but the function call is not executed until the surrounding function returns.
The deferred call's arguments are evaluated immediately, but the function call is not executed until the surrounding function returns.


<source lang='go' run>
<syntaxhighlight lang='go' run>
package main
package main


15번째 줄: 15번째 줄:
fmt.Println("hello")
fmt.Println("hello")
}
}
</source>
</syntaxhighlight>


==Stacking defers==
==Stacking defers==
22번째 줄: 22번째 줄:
To learn more about defer statements read [https://blog.golang.org/defer-panic-and-recover this blog post].
To learn more about defer statements read [https://blog.golang.org/defer-panic-and-recover this blog post].


<source lang='go' run>
<syntaxhighlight lang='go' run>
package main
package main


36번째 줄: 36번째 줄:
fmt.Println("done")
fmt.Println("done")
}
}
</source>
</syntaxhighlight>
 
==같이 보기==
* [[Go 패닉]]


==참고==
==참고==

2024년 2월 16일 (금) 13:08 기준 최신판

1 개요[ | ]

Go defer

A defer statement defers the execution of a function until the surrounding function returns.

The deferred call's arguments are evaluated immediately, but the function call is not executed until the surrounding function returns.

package main

import "fmt"

func main() {
	defer fmt.Println("world")

	fmt.Println("hello")
}

2 Stacking defers[ | ]

Deferred function calls are pushed onto a stack. When a function returns, its deferred calls are executed in last-in-first-out order.

To learn more about defer statements read this blog post.

package main

import "fmt"

func main() {
	fmt.Println("counting")

	for i := 0; i < 10; i++ {
		defer fmt.Println(i)
	}

	fmt.Println("done")
}

3 같이 보기[ | ]

4 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}