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

(새 문서: ==개요== ;Go defer <source lang='go' run> package main import "fmt" func main() { defer fmt.Println("world") fmt.Println("hello") } </source> ==참고== * https://tour.golang...)
 
 
(사용자 2명의 중간 판 3개는 보이지 않습니다)
1번째 줄: 1번째 줄:
==개요==
==개요==
;Go defer
;Go defer
A defer statement defers the execution of a function until the surrounding function returns.


<source lang='go' run>
The deferred call's arguments are evaluated immediately, but the function call is not executed until the surrounding function returns.
 
<syntaxhighlight lang='go' run>
package main
package main


12번째 줄: 15번째 줄:
fmt.Println("hello")
fmt.Println("hello")
}
}
</source>
</syntaxhighlight>
 
==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 [https://blog.golang.org/defer-panic-and-recover this blog post].
 
<syntaxhighlight lang='go' run>
package main
 
import "fmt"
 
func main() {
fmt.Println("counting")
 
for i := 0; i < 10; i++ {
defer fmt.Println(i)
}
 
fmt.Println("done")
}
</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 }}