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的更多相关文章

  1. create feature from text file

    '''---------------------------------------------------------------------------------- Tool Name: Cre ...

  2. Binary file to C array(bin2c)

    /******************************************************************************** * Binary file to C ...

  3. unity, read text file

    using System.IO; //test read txt        //Resources.Load(...) loads an asset stored at path in a Res ...

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

  5. shell脚本执行时报"bad interpreter: Text file busy"的解决方法

    在执行一个shell脚本时,遇到了“-bash: ./killSession.sh: /bin/bash: bad interpreter: Text file busy”错误提示,如下所示: [or ...

  6. eclipse的使用-------Text File Encoding没有GBK选项的设置

    eclipse的使用-------Text File Encoding没有GBK选项的设置 2013-12-25 09:48:06 标签:java myeclipse使用 有一个项目是使用GBK编码的 ...

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

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

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

随机推荐

  1. jquery设置下拉菜单

    jQuery代码 1,引用jQuery库 2,show方法 3,hide方法 <script type="text/javascript"> $function(){ ...

  2. About SQLite

    About SQLite See Also... Features When to use SQLite Frequently Asked Questions Well-known Users Boo ...

  3. js前台加密,java后台解密实现

    参考资料: JS前台加密,java后台解密实现

  4. B-index、bitmap-index、text-index使用场景详解

    索引的种类:B-tree索引.Bitmap索引.TEXT index1.  B-tree索引介绍: B-tree 是一种常见的数据结构,也称多路搜索树,并不是二叉树.B-tree 结构可以显著减少定位 ...

  5. JSON.stringify的使用方法

    语法: JSON.stringify(value [, replacer] [, space]) value:是必须要的字段.就是你输入的对象,比如数组啊,类啊等等. replacer:这个是可选的. ...

  6. Linq to Entity经验:表达式转换

    http://www.cnblogs.com/ASPNET2008/archive/2012/10/27/2742434.html 最近一年的项目,我主要负责一些小型项目(就是指企业内部的小项目),在 ...

  7. HTML5桌面通知:notification

    最近由于公司业务需要,领导要求IM消息有像网页微信那样有新消息桌面右下角弹出一个提示框的效果!由于自己才疏学浅,一时还没明白微信是怎么实现的!所以只能问百度(因为懒得FQ)咯! 在网上搜索了N久,心都 ...

  8. MYSQL权限表user操作

        MYSQL权限表user cmd中进人mysql找到mysql安装目录     E:\wamp\bin\mysql\mysql5.6.12\bin>mysql.exe -u 用户名  - ...

  9. Android应用开发-数据存储和界面展现(二)(重制版)

    SQLite数据库 // 自定义类MyOpenHelper继承自SQLiteOpenHelper MyOpenHelper oh = new MyOpenHelper(getContext(), &q ...

  10. 浅入浅出“服务器推送”之一:Comet简介

    最近有个项目,其中有项需求要从服务器端主动向客户端推送数据,本以为很简单,但在实际做的过程中发现很棘手,并没有想象中的简单.从网上搜索学习,发现主流讲的还是Ajax的长轮询技术或者流技术,websoc ...