Go 클로저 피보나치 예시

Jmnote (토론 | 기여)님의 2021년 5월 3일 (월) 16:07 판 (새 문서: ==개요== ;Go Fibonacci Closure <syntaxhighlight lang='go' run> package main import "fmt" // fib returns a function that returns // successive Fibonacci numbers. func fib() func()...)
(차이) ← 이전 판 | 최신판 (차이) | 다음 판 → (차이)

1 개요

Go Fibonacci Closure
package main

import "fmt"

// fib returns a function that returns
// successive Fibonacci numbers.
func fib() func() int {
	a, b := 0, 1
	return func() int {
		a, b = b, a+b
		return a
	}
}

func main() {
	f := fib()
	// Function calls are evaluated left-to-right.
	fmt.Println(f(), f(), f(), f(), f())
}

2 참고

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