go Language Learning - Notes 2

Fibonacci language columns go, function closure to achieve

main Package 

Import "FMT" 


FUNC Fibonacci () FUNC () {int 
        CUR: =. 1 
        next: =. 1 
        return FUNC () {int 
                RET: = CUR // record the current value of 
                cur = next // current value to the next 
                next + = ret // recomputing a 
                return ret // returns the current value before the movement 
        } 
} 

FUNC main () { 
        F: = Fibonacci () 
        for I: = 0; I <10; I ++ { 
                fmt.Println (F ( )) 
        } 
}

  

Guess you like

Origin www.cnblogs.com/gsjtu/p/12556470.html