go read text file into string array
http://stackoverflow.com/questions/5884154/golang-read-text-file-into-string-array-and-write
方法一
package main import (
"bufio"
"fmt"
"log"
"os"
) // readLines reads a whole file into memory
// and returns a slice of its lines.
func readLines(path string) ([]string, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close() var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
} // writeLines writes the lines to the given file.
func writeLines(lines []string, path string) error {
file, err := os.Create(path)
if err != nil {
return err
}
defer file.Close() w := bufio.NewWriter(file)
for _, line := range lines {
fmt.Fprintln(w, line)
} }
方法二(比较简洁,但文件不能太大)
content, err := ioutil.ReadFile(filename)
if err != nil {
//Do something
}
lines := strings.Split(string(content), "\n")
go read text file into string array的更多相关文章
- create feature from text file
'''---------------------------------------------------------------------------------- Tool Name: Cre ...
- Binary file to C array(bin2c)
/******************************************************************************** * Binary file to C ...
- unity, read text file
using System.IO; //test read txt //Resources.Load(...) loads an asset stored at path in a Res ...
- read content in a text file in python
** read text file in pythoncapability: reading =text= from a text file 1. open the IDLE text editor ...
- shell脚本执行时报"bad interpreter: Text file busy"的解决方法
在执行一个shell脚本时,遇到了“-bash: ./killSession.sh: /bin/bash: bad interpreter: Text file busy”错误提示,如下所示: [or ...
- eclipse的使用-------Text File Encoding没有GBK选项的设置
eclipse的使用-------Text File Encoding没有GBK选项的设置 2013-12-25 09:48:06 标签:java myeclipse使用 有一个项目是使用GBK编码的 ...
- Writing Text File From A Tabular Block In Oracle Forms
The example given below for writing text file or CSV using Text_IO package from a tabular block in O ...
- Change value of string array at debug eclipse--转
Question: I have an application, but to test something, I need to change value of a String[]. But wh ...
- The 12th tip of DB Query Analyzer, powerful in text file process
MA Gen feng ( Guangdong Unitoll Services incorporated, Guangzhou 510300) Abstract It's very powerf ...
随机推荐
- C#压缩图片——高质量压缩方式
传入Bitmap对象,以及新图片的长宽(Bitmap.Size),这样生成的就是跟原图尺寸一致的低质量图片 public Bitmap GetImageThumb(Bitmap mg, Size ne ...
- hdu 1010 深搜+剪枝
深度搜索 剪枝 还不是很理解 贴上众神代码 //http://blog.csdn.net/vsooda/article/details/7884772#include<iostream> ...
- ln 命令使用
今天在工作中遇到了“ln -sf”命令,发觉很久没用基本忘光,遂重拾鸟哥神书温补了一把. 简单描述的话,ln是linux中用来链接文件的,存在两种不同的连接: 1) Hard Link 实现该操作很简 ...
- 拜拜了,浮动布局-基于display:inline-block的列表布局
原创文章,转载请注明来自张鑫旭-鑫空间-鑫生活[http://www.zhangxinxu.com]本文地址:http://www.zhangxinxu.com/wordpress/?p=1194
- ArcEngine :The XY domain on the spatial reference is not set or invalid错误
在创建数据集的时候,提示The XY domain on the spatial reference is not set or invalid错误. 原因:未设置空间参考(ISpatialRefer ...
- [Linux] - Docker pure-ftp使用
Docker使用pure-ftp的方法及配置 1.用docker下载pure-ftp docker pull stilliard/pure-ftpd:hardened 2.下载完后直接运行 docke ...
- [2014.01.27]wfTextImage 文字图像组件 1.6
全新开发的文字转图像组件--wfTextImage,使用简单,功能强大,图像处理效果极佳. 将大段的文本内容转换成GIF图片. 有效防止文字内容被复制抄袭,有效保护文字资料. ...
- 项目管理-Kick OFF 简称KO
KO的内容包括以下几个过程: 1.项目背景 我们项目在哪里?说过去,做项目之前的“悲惨境地”,明确问题根源在哪里,明白为什么要做这个项目. 2.项目意义.目的与目标 我们项目去哪里?说将来,项目完成之 ...
- Query意图分析:记一次完整的机器学习过程(scikit learn library学习笔记)
所谓学习问题,是指观察由n个样本组成的集合,并根据这些数据来预测未知数据的性质. 学习任务(一个二分类问题): 区分一个普通的互联网检索Query是否具有某个垂直领域的意图.假设现在有一个O2O领域的 ...
- 通过Daffodil for VS使VS2010的IDE可以用VC6 VC7.1 VC9等编译器进行项目编译
本文内容中的部分资料和知识来源于网络,具体引用出处不明. VS的IDE从VC6到VS2010的变化可谓是天翻地覆,最新的VS2010有一个特性就是支持多显示器开发,这无疑为我们的开发带来很大的便利. ...