Let's have some fun with functions.

Implement a fibonacci function that returns a function (a closure) that returns successive fibonacci numbers.

package main

import "fmt"

// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
before :=
now :=
return func() int {
if before == {
before++
return before
}
if now == {
now++
return now
}
temp := now
now = before + now
before = temp
return now
}
} func main() {
f := fibonacci()
for i := ; i < ; i++ {
fmt.Println(f())
}
}

A Tour of Go Exercise: Fibonacci closure的更多相关文章

  1. A Tour of Go Exercise: Images

    Remember the picture generator you wrote earlier? Let's write another one, but this time it will ret ...

  2. A Tour of Go Exercise: HTTP Handlers

    Implement the following types and define ServeHTTP methods on them. Register them to handle specific ...

  3. A Tour of Go Exercise: Errors

    Copy your Sqrt function from the earlier exercises and modify it to return an error value. Sqrt shou ...

  4. A Tour of Go Exercise: Maps

    Implement WordCount. It should return a map of the counts of each “word” in the string s. The wc.Tes ...

  5. A Tour of Go Exercise: Slices

    Implement Pic. It should return a slice of length dy, each element of which is a slice of dx 8-bit u ...

  6. A Tour of Go Exercise: Loops and Functions

    As a simple way to play with functions and loops, implement the square root function using Newton's ...

  7. exercise.tour.go google的go官方教程答案

    /* Exercise: Loops and Functions #43 */ package main import ( "fmt" "math" ) fun ...

  8. go 准备

    坚持每天抽点时间 学习联系 go 语法 主要参考 https://tour.golang.org 官方导向,英语不好的可以切换到中文版本.这个之前都是墙外面的,只能访问国内映像地址 吐槽一下就是 里面 ...

  9. Go for Pythonistas Go and the Zen of Python 禅

    Go for Pythonistas https://talks.golang.org/2013/go4python.slide#1 Things I don't like about Python ...

随机推荐

  1. *gravity的取值详表

    android有 android:layout_gravity 和 android:gravity,前者设置相对父控件布局,后者是设置自己内部的控件的布局. Value Description top ...

  2. mac terminal终端ls命令参数详解

    原文:https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/ls.1.html -a ...

  3. jquery网页倒计时效果,秒杀,限时抢购!

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  4. [原]Unity3D深入浅出 - 认识开发环境中的GameObject菜单栏

    Create Empty:创建空对象 Create Other:创建其他对象 Particle System:创建粒子系统 Camera:创建相机 GUI Text:GUI文本 GUI Texture ...

  5. Windows如何安装自定义服务

    系统:Windows8 开发环境:Visual Studio 2012 开发平台 .Net Framework 4.5 找到系统盘的 C:\Windows\Microsoft.NET\Framewor ...

  6. vijos1603迷宫

    这题的构思太巧妙了: 经典题目8 给定一个有向图,问从A点恰好走k步(允许重复经过边)到达B点的方案数mod p的值    把给定的图转为邻接矩阵,即A(i,j)=1当且仅当存在一条边i->j. ...

  7. 跟我学LFS LiveUSB制作

    LFS LiveCD启动 插入U盘,查看U盘相应的设备名 $ sudo /sbin/fdisk -l ... Device Boot      Start         End      Block ...

  8. [ZOJ 2836] Number Puzzle

    Number Puzzle Time Limit: 2 Seconds      Memory Limit: 65536 KB Given a list of integers (A1, A2, .. ...

  9. LeetCode Product of Array Except Self (除自身外序列之积)

    题意:给一个序列nums,要求返回一个序列ans,两序列元素个数相同,ans第i个元素就是除了nums[i]之外所有的数相乘之积. 思路:时间O(n),额外空间O(0). 第一次扫一遍,处理nums[ ...

  10. Scrum 体验活动笔记

    2014-03-10  Isoftstone library 1.识别角色(用户),形象 :名称.痛处.属性.需求 2.编写故事 story以验证需求是否正确:我们假设(客户)  进行验证结果... ...