A Tour of Go Exercise: Fibonacci closure
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的更多相关文章
- A Tour of Go Exercise: Images
Remember the picture generator you wrote earlier? Let's write another one, but this time it will ret ...
- A Tour of Go Exercise: HTTP Handlers
Implement the following types and define ServeHTTP methods on them. Register them to handle specific ...
- A Tour of Go Exercise: Errors
Copy your Sqrt function from the earlier exercises and modify it to return an error value. Sqrt shou ...
- 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 ...
- 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 ...
- 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 ...
- exercise.tour.go google的go官方教程答案
/* Exercise: Loops and Functions #43 */ package main import ( "fmt" "math" ) fun ...
- go 准备
坚持每天抽点时间 学习联系 go 语法 主要参考 https://tour.golang.org 官方导向,英语不好的可以切换到中文版本.这个之前都是墙外面的,只能访问国内映像地址 吐槽一下就是 里面 ...
- 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 ...
随机推荐
- Android 使用httpClient POST 模拟发送 multipart表单内容
使用的环境:apache-mime4j-0.6.jar,httpcore-4.3.2.jar,httpmime-4.3.3.jar try { HttpPost httpPost = new Http ...
- php关于private、public成员变量访问问题
如果类里面定义了__get($name)方法,则不论类的private成员还是public成员,都能够在类的外面通过类似$class->name访问到.如果是public变量,则不会自动调用ge ...
- jmeter 响应结果分析二
转自:http://www.cnblogs.com/Carrie_Liang/archive/2008/11/10/1330997.html 前文再续,续接上一回.上一篇讲了如何利用Assertion ...
- android 安全需要关注
1.通过签名校验保护,能有效避免应用被二次打包,杜绝盗版应用的产生2.对内存数据进行变换处理和动态跟踪,有效防止数据被获取和修改3.对代码进行加密压缩,可防止破解者还原真实代码逻辑,避免被复制4.多重 ...
- LoadImage 和 BitBlt
#include <windows.h> #define WINDOWCLASS TEXT("Test") #define WNDTITLE TEXT("Te ...
- mysql 闪回表工具
use HTTP::Date qw(time2iso str2time time2iso time2isoz); use POSIX; my $SDATE = strftime("%Y-%m ...
- devfs,proc,udev
devfs:常用的驱动函数封装 proc:在用户态检查内核状态的机制 udev 和 devfs相比? 一个是用户空间里的,一个运行在内核空间且被2.6以后版本抛弃了
- C#中配置文件的使用
1. 向项目添加app.config文件: 右击项目名称,选择“添加”→“添加新建项”,在出现的“添加新项”对话框中,选择“添加应用程序配置文件”:如果项目以前没有配置文件,则默认的文件名称为“app ...
- 【HDOJ】4605 Magic Ball Game
思路1:树状数组+离线处理,对所有的w离散化处理,边dfs边使用树状数组更新左右w的情况.思路2:主席树,边bfs边建树.结点信息存储cnt,然后在线查询.树状数组. /* 4605 */ #incl ...
- php 对象调用方法
static union _zend_function *zend_std_get_method(zval **object_ptr, char *method_name, int method_le ...