使用bufio包和函数式变成实现类似python生成器效果
package main
import (
"bufio"
"fmt"
"io"
"strings"
)
type intGen func() int
func (g intGen) Read(p []byte) (n int, err error) {
next := g()
if next > 10000 { //因为是斐波那契额数列,所以取不完,所以设置让他取完
return 0, io.EOF
}
s := fmt.Sprintf("%d\n", next)
return strings.NewReader(s).Read(p) //通过这个方法将字符串写入到缓冲区里面去
}
func fibonacci() intGen {
a, b := 0, 1
return func() int {
a, b = b, a+b
return b
}
}
func PrintFibonacci(gen intGen) {
scanner := bufio.NewScanner(gen) //实现了Read方法,也就实现了io.Writer接口
for scanner.Scan() {
fmt.Println(scanner.Text())
}
}
func main() {
f := fibonacci()
PrintFibonacci(f)
}
使用bufio包和函数式变成实现类似python生成器效果的更多相关文章
- golang import all 类似python import * 效果
import "io/ioutil" func main() { content, err = iotuil.ReadFile("somefile.txt") ...
- Golang学习 - bufio 包
------------------------------------------------------------ // bufio 包实现了带缓存的 I/O 操作 -------------- ...
- golang之bufio包的使用
原文地址:http://www.niu12.com/article/38 github地址:https://github.com/ZQCard/go_api_practice // 参考:https: ...
- ViewPagerWithRecyclerDemo【RecyclerView+ViewPager实现类似TabLayout+ViewPager效果】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 使用RecyclerView+ViewPager实现类似TabLayout+ViewPager效果. 效果图 使用步骤 一.项目组织 ...
- Android v7包下Toolbar和ActionBarActivity实现后退导航效果
android.support.v7包下的ToolBar和ActionBarActivity,均自带后退导航按钮,只是要手动开启,让它显示出来.先来看看ToolBar,页面前台代码: <andr ...
- 基于CSS3新属性Animation及transform实现类似翻书效果
注:本实例JS部分均以原生JS编写,不善用原生JS的,可用jQuery等对三方框架改写 先上效果图:(样式有点丑,可以忽略一下下,效果出来了就好,后期加到其他项目中方便更改0.0) 类似翻书效果,原本 ...
- [DevExpress]利用LookUpEdit实现类似自动提示效果
原文:[DevExpress]利用LookUpEdit实现类似自动提示效果 关键代码: public static void BindWithAutoCompletion(this LookUpEdi ...
- Android类似Periscope点赞效果
原文 https://github.com/AlanCheen/PeriscopeLayout 主题 安卓开发 PeriscopeLayout A layout with animation li ...
- Android 两种方式实现类似水波扩散效果
原文链接 https://mp.weixin.qq.com/s/M19tp_ShOO6esKdozi7Nlg 两种方式实现类似水波扩散效果,先上图为敬 自定义view实现 动画实现 自定义view实现 ...
随机推荐
- Selenium 配置IE浏览器
1.安装selenium pip install selenium 2.安装IE浏览器driver http://selenium-release.storage.googleapis.com/ind ...
- [高清] Java编程思想第四版完整中文高清版
------ 郑重声明 --------- 资源来自网络,纯粹共享交流, 如果喜欢,请您务必支持正版!! --------------------------------------------- 下 ...
- Java JDK1.8源码学习之路 1 Object
写在最前 对于一个合格的后端程序员来说,现行的流行框架早已经能胜任基本的企业开发,Springboot 任何的框架都把重复的工作更佳简单/优化的解决掉,但是完全陷入在这样的温水里面, 好比温水煮青蛙, ...
- linq 动态排序 order by
项目查询数据库使用的是linq 语法,可是后期需要用到不同字段的排序.各种纠结! 在网上找了各种资料 后面才找到两种方法 using System; using System.Collections. ...
- Bootstrap-table 使用总结,和其参数说明
转载于:https://www.cnblogs.com/laowangc/p/8875526.html 一.什么是Bootstrap-table? 在业务系统开发中,对表格记录的查询.分页.排序等处理 ...
- C# 值类型和引用类型等值判断
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- C# winform导出数据弹出可修改目录及文件名的窗口
string localFilePath = "", fileNameExt = "", newFileName = "", FilePat ...
- [服务器]SSL安装证书教程
来自阿里云教程 Tomcat服务器安装SSL证书 安装PFX格式证书 https://help.aliyun.com/document_detail/98576.html?spm=a2c4g.1118 ...
- docker入门一:docker安装(在线跟离线)
一.在线安装 1.安装依赖 yum install -y yum-utils device-mapper-persistent-data lvm2 2.添加软件源 yum-config-manager ...
- HashMap,HashSet
HashMap,HashSet 摘自:https://www.cnblogs.com/skywang12345/p/3310887.html#a1 目录 一. HashMap(键值对形式存取,键 ...