go read text file into string array
http://stackoverflow.com/questions/5884154/golang-read-text-file-into-string-array-and-write
方法一
1 package main 2 3 import ( 4 "bufio" 5 "fmt" 6 "log" 7 "os" 8 ) 9 10 // readLines reads a whole file into memory 11 // and returns a slice of its lines. 12 func readLines(path string) ([]string, error) { 13 file, err := os.Open(path) 14 if err != nil { 15 return nil, err 16 } 17 defer file.Close() 18 19 var lines []string 20 scanner := bufio.NewScanner(file) 21 for scanner.Scan() { 22 lines = append(lines, scanner.Text()) 23 } 24 return lines, scanner.Err() 25 } 26 27 // writeLines writes the lines to the given file. 28 func writeLines(lines []string, path string) error { 29 file, err := os.Create(path) 30 if err != nil { 31 return err 32 } 33 defer file.Close() 34 35 w := bufio.NewWriter(file) 36 for _, line := range lines { 37 fmt.Fprintln(w, line) 38 } 39 40 }
方法二(比较简洁,但文件不能太大)
1 content, err := ioutil.ReadFile(filename) 2 if err != nil { 3 //Do something 4 } 5 lines := strings.Split(string(content), "\n")
转载于:https://www.cnblogs.com/yanlixin/p/4732732.html
go read text file into string array相关推荐
- python udp 大文件_Python UDP服务器发送文本文件的行(Python UDP Server send lines of a text file)...
Python UDP服务器发送文本文件的行(Python UDP Server send lines of a text file) 我需要模拟一个UDP服务器,它在无限循环中逐行发送文本文件的内容. ...
- python里面的几个用法,not in,c if a else b,[fun(a) for a in [...]] , a,b=b,a,'内容'.join([string array])
1.not in >>> a=2 >>> a not in [2,3,4] False >>> a in [2,3,4] 2. c if a el ...
- a b c d java_java中数组String array{a,b,c,d,c,d,e}如何能把字段相同的分到另外的数组中...
展开全部 数组中提取相同的字母,可以使用string的charat方法遍历字母,放到一个string中,找到之后删除掉这62616964757a686964616fe59b9ee7ad94313333 ...
- 成功解决CondaError: Error reading file, file should be a text file containing packages conda create --he
成功解决CondaError: Error reading file, file should be a text file containing packages conda create --he ...
- linux文件夹重命名busy,Linux下执行程序出现 Text file busy 提示时的解决方法
使用 fuser xxx 命令查看制定的文件被哪个进程占用,然后关闭该进程,则解决问题. 备注: 如果 xxx 命令是在普通用root 下开启, 而在普通用户下使用:fuser xxx 是没有结果的 ...
- 6种字符串数组的java排序 (String array sort)
注意,本文不是字符串排序,是字符串数组的排序. 方法分别是: 1.低位优先键索引排序 2.高位优先建索引排序 3.Java自带排序(经过调优的归并排序) 4.冒泡排序 5.快速排序 6.三向快速排序 ...
- c语言学生管理系统write to a text file,语言编程学生成绩管理系统.doc
语言编程学生成绩管理系统 c语言编程-学生成绩管理系统 #include /*引用库函数*/ #include #include #include typedef struct /*定义结构体数组*/ ...
- shell脚本执行时报bad interpreter: Text file busy的解决方法
在执行一个shell脚本时,遇到了"-bash: ./killSession.sh: /bin/bash: bad interpreter: Text file busy"错误提示 ...
- shell脚本执行时报“bad interpreter: Text file busy“的解决方法
shell脚本执行时报"bad interpreter: Text file busy"的解决方法 参考文章: (1)shell脚本执行时报"bad interprete ...
最新文章
- Redis学习第三课:Redis Hash类型及操作
- android手机用户,[信息图]Android手机用户如何使用自己的设备?
- android拍照截图组件,Android_Android实现从相册截图的功能,在这篇文章中,我将向大家展 - phpStudy...
- algorithm design manual: 8-19
- 自学Android资料大放送
- Atitit 提升水平 把代码写的有技术含量 目录 1. 提高可读性(重要)	2 1.1. 异常模式代替返回值	2 1.2. Dsl	2 1.3. 流畅接口方法链	2 1.4. 层次结构抽象	2 1
- win7共享中心服务器运行失败,Win7系统网络与共享中心无法打开怎样解决?
- DDSM数据处理之PngWithOverlay 框出病灶区域
- 园区网络—中小型企业网络工程项目实践(思科模拟器)
- Selenium Chrome设置代理之后验证依旧是本机ip原因
- MacBook解决控制台图标问题
- 堆中存什么?栈中存什么?
- 一篇文章扫盲手机SIM卡相关知识
- ***虚拟专用网技术
- PS海绵工具加深工具简单工具的区别
- 在HBase数据库创建表
- 个推的推送消息延迟的一个原因记录
- 多自由度系统模态分析与试验
- Button字体大写
- LABVIEW学习之(2)读写串口(VISA)
热门文章
- oracle 根据用户备份,Oracle基于用户的备份及恢复
- cmd查询Oracle中的表 成表格显示,oracle如何通过cmd导出某个用户下的所有表
- 什么是传感器? 传感器由哪几部分组成? 传感器分类?
- 基于JavaWeb SSM mybatis 学生信息管理系统设计和实现以及文档报告
- 数控机床需要c语言程序吗,数控机床编程与操作--详细介绍
- Java存储图书信息
- Spring Boot swagger之前后端分离
- matlab电机标定,基于模型的电机标定及高精度电机建模
- java inputstream read_20191209-java部分流处理
- HTML5学习笔记(二)