go语言io和ioutil包的学习和使用
io包
- package main;
- import (
- "errors"
- "fmt"
- "io"
- )
- //io包中定义了非常多的interface
- //只要实现了接口中的方法
- //那么io包中的导出方法就可以传入我们自定义的对象然后进行处理
- //像什么文件数据,网络数据,数据库数据都可以统一操作接口
- type MyReWr struct {
- //保存的数据
- data string;
- //指向当前数据读取的位置下标
- rix int;
- //指向当前数据写入的位置下标
- wix int;
- }
- func MyReWrNew(s string) *MyReWr {
- return &MyReWr{s, 0, 0};
- }
- //读取数据到p中
- func (m *MyReWr) Read(p []byte) (n int, err error) {
- l := len(p);
- num := 0;
- if m.rix >= len(m.data) {
- return 0, errors.New("EOF");
- }
- tmp := []byte(m.data);
- //判断当前数据读取的下标
- if (m.rix + l) < len(m.data) {
- num = l;
- } else {
- num = len(m.data) - m.rix;
- }
- //循环给p设置数据
- for ix := 0; ix < num; ix++ {
- p[ix] = tmp[m.rix+ix];
- }
- //增加读取下标
- m.rix = m.rix + num;
- return num, nil;
- }
- //将p中数据写入
- func (m *MyReWr) Write(p []byte) (n int, err error) {
- l := len(p);
- num := 0;
- if m.wix >= len(m.data) {
- return 0, errors.New("EOF");
- }
- tmp := []byte(m.data);
- //判断当前数据写入的下标
- if (m.wix + l) < len(m.data) {
- num = l;
- } else {
- num = len(m.data) - m.wix;
- }
- //循环写入数据
- for ix := 0; ix < num; ix++ {
- tmp[m.wix+ix] = p[ix];
- }
- m.data = string(tmp);
- //增加写入下标
- m.wix = m.wix + num;
- return num, nil;
- }
- func main() {
- //我们自定义的一个结构,实现了Read和Write方法
- m := MyReWrNew("12345678910");
- p := make([]byte, 3);
- //循环读取数据
- for {
- n, _ := m.Read(p);
- if n == 0 {
- break;
- }
- fmt.Println(n, string(p[0:n]));
- }
- //循环写入数据
- for {
- n, _ := m.Write([]byte("111"));
- if n == 0 {
- break;
- }
- fmt.Println(n, m.data);
- }
- //MyReWr结构就可以使用如下方法
- m2 := MyReWrNew("666666666");
- m3 := MyReWrNew("999999999");
- tee := io.TeeReader(m2, m3);
- for {
- //循环从m2中读取数据到p中,再写入到m3中。
- n, _ := tee.Read(p);
- if n == 0 {
- break;
- }
- fmt.Println(m2, m3);
- }
- //向m4中拷贝m5中的数据
- m4 := MyReWrNew("aaaaaaaaa");
- m5 := MyReWrNew("mmmmmmm");
- io.Copy(m4, m5);
- fmt.Println(m4, m5);
- //从m6中读取数据放入p2中
- m6 := MyReWrNew("abcdefghijklmo");
- p2 := make([]byte, len(m6.data));
- io.ReadFull(m6, p2);
- fmt.Println(string(p2));
- //向m7中写入字符串,如果实现了WriteString方法会直接调用。
- m7 := MyReWrNew("hello");
- io.WriteString(m7, "world123");
- fmt.Println(m7);
- }
ioutil包
- package main;
- import (
- "strings"
- "io/ioutil"
- "fmt"
- )
- func main() {
- //NopCloser返回一个读取对象的ReadCloser接口
- //用于提供Close方法
- r := strings.NewReader("hello");
- rcl := ioutil.NopCloser(r);
- defer rcl.Close();
- //ReadAll读取所有数据
- r2 := strings.NewReader("1234567890");
- p, _ := ioutil.ReadAll(r2);
- fmt.Println(string(p));
- //读取目录下信息
- fileInfo, _ := ioutil.ReadDir("./");
- for _, v := range fileInfo {
- fmt.Println(v.Name());
- }
- //读取整个文件数据
- data, _ := ioutil.ReadFile("./1.txt");
- fmt.Println(string(data));
- //向指定文件写入数据,如果文件不存在,则创建文件,写入数据之前清空文件
- ioutil.WriteFile("./xxx.txt", []byte("hello,world"), 0655);
- //在当前目录下,创建一个以test为前缀的临时文件夹,并返回文件夹路径
- name, _ := ioutil.TempDir("./", "test");
- fmt.Println(name);
- //在当前目录下,创建一个以test为前缀的文件,并以读写模式打开文件,并返回os.File指针
- file, _ := ioutil.TempFile("./", "test");
- file.WriteString("写入字符串");
- file.Close();
- }
go语言io和ioutil包的学习和使用的更多相关文章
- go语言学习笔记---读取文件io/ioutil 包
io/ioutil 包几个函数方法 名称 作用 备注 ReadAll 读取数据,返回读到的字节 slice 1 ReadDir 读取一个目录,返回目录入口数组 []os.FileInfo, 2 Re ...
- go语言path包和filepath包的学习与使用
path包的使用 package main; import ( "fmt" "path" ) //go语言path包的学习 func main() { //返回 ...
- ioutil包二
ioutil包二 (原创随笔,转载请注明出处 http://www.cnblogs.com/majianguo/p/8016426.html) ioutil包实现了一些I/O实用功能,导出了7个函数和 ...
- Linux IO操作——RIO包
1.linux基本I/O接口介绍 ssize_t read(int fd, void *buf, size_t count); ssize_t write(int fd, void *buf, siz ...
- R语言中的机器学习包
R语言中的机器学习包 Machine Learning & Statistical Learning (机器学习 & 统计学习) 网址:http://cran.r-project ...
- Go语言基础之Path包与FilePath包
文章引用自 path包的使用 package main; import ( "fmt" "path" ) //go语言path包的学习 func main() ...
- 1.go语言入门----Helloworld与包引用
HelloWorld与包引用 学习一门语言的惯例都是从helloworld开始,go语言也不例外 在gopath下的src中创建一个helloworld目录,创建main.go文件 package m ...
- GO系列-ioutil包
ioutil包提供给外部使用的一共有1个变量,7个方法. // Discard 是一个 io.Writer 接口,调用它的 Write 方法将不做任何事情 // 并且始终成功返回. var Disca ...
- R----stringr包介绍学习
1. stringr介绍 stringr包被定义为一致的.简单易用的字符串工具集.所有的函数和参数定义都具有一致性,比如,用相同的方法进行NA处理和0长度的向量处理. 字符串处理虽然不是R语言中最主要 ...
随机推荐
- Oracle数据文件迁移到裸设备
本文主要描述如何将Oracle表空间的文件系统形式的数据文件迁移到LV裸设备上. 前提条件 1.oracle运行正常. 2.已使用LVM命令规划好LV文件.如/dev/vgoracle/lvdatat ...
- git-03 建立分支
git branch han git checkout git push origin han
- http://wenku.baidu.com/view/26afdb8371fe910ef12df8ccRevit采用DWG和FBX两种格式导入3D max方法的总结
2.DWG 属性——导出为 ACIS 实体(如果选择导出为多边形网格,则将每个图元导出为 由多个多边形组成的对象,这些多边形相互连接或组成 “ 网格 ” .在导出到 DWG 文件以用 于 Ma ...
- StarRatingBar星星切换动画《IT蓝豹》
StarRatingBar星星切换动画 StarRatingBar星星切换动画,很久没有学习一下这个RatingBar了,今天来看看这个RatingBar的动画切换效果,本例子主要是RatingBar ...
- thymeleaf 字符串的拼接
- Android文档 学习目录
Building Your First App After you've installed the Android SDK, start with this class to learn the b ...
- C# HttpWebRequest 错误总结
1.form data 需要编码!!! byte[] data = new ASCIIEncoding().GetBytes("pattern=0&wwid=古兴越&good ...
- js判断json对象中是否含有某个属性
obj.hasOwnProperty("key"); 原地址:https://blog.csdn.net/feicongcong/article/details/53463872
- Webpack Loaders
[Webpack Loaders] 1.Query parameters Loader can be passed query parameters via a query string (just ...
- CircleImageView of Android
[CircleImageView of Android] github上有一个开源的圆角图片项目.地址:https://github.com/hdodenhof/CircleImageView 使用分 ...