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. 其实,前面倒腾那么多,只是为了想玩SPRING BOOT

    嘿嘿,,曲线达到.. 看来看来很多国内的速成,都不爽. 官方教程最体贴~~~:) http://docs.spring.io/spring-boot/docs/current/reference/ht ...

  2. winform自定义文件程序-- 不允许所请求的注册表访问权(ZSSQL)

    常见问题1: 不允许所请求的注册表访问权 win7.win8 双击程序文件ZSSQL时候会出现 不允许所请求的注册表访问权 的弹窗异常 解决方法:ZSSQL.exe 右键 属性--兼容性--以管理员身 ...

  3. Repeater, DataList, 和GridView控件的区别

    http://blog.sina.com.cn/s/blog_646dc75c0100h5p6.html http://www.cnblogs.com/phone/archive/2010/09/15 ...

  4. linux下添加PATH环境变量

    添加PATH环境变量,第1种方法:[root@lx_web_s1 ~]# export PATH=/usr/local/webserver/mysql/bin:$PATH 再次查看: [root@lx ...

  5. HeadFirst设计模式之单例模式

    一. 1.The Singleton Pattern ensures a class has only one instance, and provides a global point of acc ...

  6. MSSQLServer基础02(SQL语句入门(脚本、命令))

    SQL 全名是结构化查询语言(Structured Query Language),是关系数据库管理系统的标准语言 SQL语句是和DBMS“交谈”专用的语句,不同DBMS都认SQL语法. SQL语句中 ...

  7. Eclipse中安装使用SVN

    参考网址: Eclipse中使用SVN - 流逝的是岁月,沉淀的是经典 - 博客频道 - CSDN.NET http://blog.csdn.net/v123411739/article/detail ...

  8. sudo: ./sd_fusing.sh:找不到命令

    1. -----

  9. Factorial

    Factorial  计算阶乘 In mathematics, the factorial of a non-negative integer n, denoted by n!, is the pro ...

  10. poj3270Cow Sorting(置换)

    链接 对于组合数学是一点也不了解 讲解 重要一点 要知道一个循环里最少的交换次数是m-1次 . #include <iostream> #include<cstdio> #in ...