io包

  1. package main;
  2.  
  3. import (
  4. "errors"
  5. "fmt"
  6. "io"
  7. )
  8.  
  9. //io包中定义了非常多的interface
  10. //只要实现了接口中的方法
  11. //那么io包中的导出方法就可以传入我们自定义的对象然后进行处理
  12. //像什么文件数据,网络数据,数据库数据都可以统一操作接口
  13.  
  14. type MyReWr struct {
  15. //保存的数据
  16. data string;
  17. //指向当前数据读取的位置下标
  18. rix int;
  19. //指向当前数据写入的位置下标
  20. wix int;
  21. }
  22.  
  23. func MyReWrNew(s string) *MyReWr {
  24. return &MyReWr{s, 0, 0};
  25. }
  26.  
  27. //读取数据到p中
  28. func (m *MyReWr) Read(p []byte) (n int, err error) {
  29. l := len(p);
  30. num := 0;
  31. if m.rix >= len(m.data) {
  32. return 0, errors.New("EOF");
  33. }
  34. tmp := []byte(m.data);
  35. //判断当前数据读取的下标
  36. if (m.rix + l) < len(m.data) {
  37. num = l;
  38. } else {
  39. num = len(m.data) - m.rix;
  40. }
  41. //循环给p设置数据
  42. for ix := 0; ix < num; ix++ {
  43. p[ix] = tmp[m.rix+ix];
  44. }
  45. //增加读取下标
  46. m.rix = m.rix + num;
  47. return num, nil;
  48. }
  49.  
  50. //将p中数据写入
  51. func (m *MyReWr) Write(p []byte) (n int, err error) {
  52. l := len(p);
  53. num := 0;
  54. if m.wix >= len(m.data) {
  55. return 0, errors.New("EOF");
  56. }
  57. tmp := []byte(m.data);
  58. //判断当前数据写入的下标
  59. if (m.wix + l) < len(m.data) {
  60. num = l;
  61. } else {
  62. num = len(m.data) - m.wix;
  63. }
  64. //循环写入数据
  65. for ix := 0; ix < num; ix++ {
  66. tmp[m.wix+ix] = p[ix];
  67. }
  68. m.data = string(tmp);
  69. //增加写入下标
  70. m.wix = m.wix + num;
  71. return num, nil;
  72. }
  73.  
  74. func main() {
  75. //我们自定义的一个结构,实现了Read和Write方法
  76. m := MyReWrNew("12345678910");
  77. p := make([]byte, 3);
  78. //循环读取数据
  79. for {
  80. n, _ := m.Read(p);
  81. if n == 0 {
  82. break;
  83. }
  84. fmt.Println(n, string(p[0:n]));
  85. }
  86. //循环写入数据
  87. for {
  88. n, _ := m.Write([]byte("111"));
  89. if n == 0 {
  90. break;
  91. }
  92. fmt.Println(n, m.data);
  93. }
  94.  
  95. //MyReWr结构就可以使用如下方法
  96. m2 := MyReWrNew("666666666");
  97. m3 := MyReWrNew("999999999");
  98. tee := io.TeeReader(m2, m3);
  99. for {
  100. //循环从m2中读取数据到p中,再写入到m3中。
  101. n, _ := tee.Read(p);
  102. if n == 0 {
  103. break;
  104. }
  105. fmt.Println(m2, m3);
  106. }
  107.  
  108. //向m4中拷贝m5中的数据
  109. m4 := MyReWrNew("aaaaaaaaa");
  110. m5 := MyReWrNew("mmmmmmm");
  111. io.Copy(m4, m5);
  112. fmt.Println(m4, m5);
  113.  
  114. //从m6中读取数据放入p2中
  115. m6 := MyReWrNew("abcdefghijklmo");
  116. p2 := make([]byte, len(m6.data));
  117. io.ReadFull(m6, p2);
  118. fmt.Println(string(p2));
  119.  
  120. //向m7中写入字符串,如果实现了WriteString方法会直接调用。
  121. m7 := MyReWrNew("hello");
  122. io.WriteString(m7, "world123");
  123. fmt.Println(m7);
  124. }

ioutil包

  1. package main;
  2.  
  3. import (
  4. "strings"
  5. "io/ioutil"
  6. "fmt"
  7. )
  8.  
  9. func main() {
  10. //NopCloser返回一个读取对象的ReadCloser接口
  11. //用于提供Close方法
  12. r := strings.NewReader("hello");
  13. rcl := ioutil.NopCloser(r);
  14. defer rcl.Close();
  15.  
  16. //ReadAll读取所有数据
  17. r2 := strings.NewReader("1234567890");
  18. p, _ := ioutil.ReadAll(r2);
  19. fmt.Println(string(p));
  20.  
  21. //读取目录下信息
  22. fileInfo, _ := ioutil.ReadDir("./");
  23. for _, v := range fileInfo {
  24. fmt.Println(v.Name());
  25. }
  26.  
  27. //读取整个文件数据
  28. data, _ := ioutil.ReadFile("./1.txt");
  29. fmt.Println(string(data));
  30.  
  31. //向指定文件写入数据,如果文件不存在,则创建文件,写入数据之前清空文件
  32. ioutil.WriteFile("./xxx.txt", []byte("hello,world"), 0655);
  33.  
  34. //在当前目录下,创建一个以test为前缀的临时文件夹,并返回文件夹路径
  35. name, _ := ioutil.TempDir("./", "test");
  36. fmt.Println(name);
  37.  
  38. //在当前目录下,创建一个以test为前缀的文件,并以读写模式打开文件,并返回os.File指针
  39. file, _ := ioutil.TempFile("./", "test");
  40. file.WriteString("写入字符串");
  41. file.Close();
  42. }

  

go语言io和ioutil包的学习和使用的更多相关文章

  1. go语言学习笔记---读取文件io/ioutil 包

    io/ioutil 包几个函数方法 名称  作用 备注 ReadAll 读取数据,返回读到的字节 slice 1 ReadDir 读取一个目录,返回目录入口数组 []os.FileInfo, 2 Re ...

  2. go语言path包和filepath包的学习与使用

    path包的使用 package main; import ( "fmt" "path" ) //go语言path包的学习 func main() { //返回 ...

  3. ioutil包二

    ioutil包二 (原创随笔,转载请注明出处 http://www.cnblogs.com/majianguo/p/8016426.html) ioutil包实现了一些I/O实用功能,导出了7个函数和 ...

  4. 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 ...

  5. R语言中的机器学习包

    R语言中的机器学习包   Machine Learning & Statistical Learning (机器学习 & 统计学习)  网址:http://cran.r-project ...

  6. Go语言基础之Path包与FilePath包

    文章引用自 path包的使用 package main; import ( "fmt" "path" ) //go语言path包的学习 func main() ...

  7. 1.go语言入门----Helloworld与包引用

    HelloWorld与包引用 学习一门语言的惯例都是从helloworld开始,go语言也不例外 在gopath下的src中创建一个helloworld目录,创建main.go文件 package m ...

  8. GO系列-ioutil包

    ioutil包提供给外部使用的一共有1个变量,7个方法. // Discard 是一个 io.Writer 接口,调用它的 Write 方法将不做任何事情 // 并且始终成功返回. var Disca ...

  9. R----stringr包介绍学习

    1. stringr介绍 stringr包被定义为一致的.简单易用的字符串工具集.所有的函数和参数定义都具有一致性,比如,用相同的方法进行NA处理和0长度的向量处理. 字符串处理虽然不是R语言中最主要 ...

随机推荐

  1. Oracle数据文件迁移到裸设备

    本文主要描述如何将Oracle表空间的文件系统形式的数据文件迁移到LV裸设备上. 前提条件 1.oracle运行正常. 2.已使用LVM命令规划好LV文件.如/dev/vgoracle/lvdatat ...

  2. git-03 建立分支

    git branch han git checkout git push origin han

  3. http://wenku.baidu.com/view/26afdb8371fe910ef12df8ccRevit采用DWG和FBX两种格式导入3D max方法的总结

        2.DWG 属性——导出为 ACIS 实体(如果选择导出为多边形网格,则将每个图元导出为 由多个多边形组成的对象,这些多边形相互连接或组成 “ 网格 ” .在导出到 DWG 文件以用 于 Ma ...

  4. StarRatingBar星星切换动画《IT蓝豹》

    StarRatingBar星星切换动画 StarRatingBar星星切换动画,很久没有学习一下这个RatingBar了,今天来看看这个RatingBar的动画切换效果,本例子主要是RatingBar ...

  5. thymeleaf 字符串的拼接

  6. Android文档 学习目录

    Building Your First App After you've installed the Android SDK, start with this class to learn the b ...

  7. C# HttpWebRequest 错误总结

    1.form data 需要编码!!! byte[] data = new ASCIIEncoding().GetBytes("pattern=0&wwid=古兴越&good ...

  8. js判断json对象中是否含有某个属性

    obj.hasOwnProperty("key"); 原地址:https://blog.csdn.net/feicongcong/article/details/53463872

  9. Webpack Loaders

    [Webpack Loaders] 1.Query parameters Loader can be passed query parameters via a query string (just ...

  10. CircleImageView of Android

    [CircleImageView of Android] github上有一个开源的圆角图片项目.地址:https://github.com/hdodenhof/CircleImageView 使用分 ...