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. 安装第三方Python模块,增加InfoPi的健壮性

    这3个第三方Python模块是可选的,不安装的话InfoPi也可以运行. 但是如果安装了,会增加InfoPi的健壮性. 目录 1.cchardet    自动检测文本编码 2.lxml    用于解析 ...

  2. LeetCode---Binary Search

    475. Heaters 思路:每趟循环查找离房子最近的热水器,计算距离,最后取最大距离 public int findRadius(int[] houses, int[] heaters) { Ar ...

  3. 别再为了this发愁了------JS中的this机制

    别再为了this发愁了------JS中的this机制 题记:JavaScript中有很多令人困惑的地方,或者叫做机制.但是,就是这些东西让JavaScript显得那么美好而与众不同.比方说函数也是对 ...

  4. iPhone4下window各个部分的高度

  5. Access 数据库连接 字符串

    <!--Microsoft.Practices.EnterpriseLibrary.Data.dll 操作引用程序集--> <connectionStrings> <ad ...

  6. AngularJs之$scope对象(作用域)

      一.作用域 AngularJs中的$scope对象是模板的域模型,也称为作用域实例.通过为其属性赋值,可以传递数据给模板渲染. 每个$scope都是Scope类的实例,Scope类有很多方法,用于 ...

  7. 在ESXi 5.x 和 ESXi 6.0.x 中如何安装第三方供应商开发的驱动程序

    在 VMware ESXi 5.x 和 ESXi 6.0.x 中如何下载并安装异步驱动程序 (2076262)   Symptoms 免责声明:本文为 How to download and inst ...

  8. TextView 获取行数,某一行的内容,某行的宽度

    获取行数 ViewTreeObserver vto = textView.getViewTreeObserver(); vto.addOnGlobalLayoutListener(new OnGlob ...

  9. json 与jsonp 特点及区别

    简单描述JSON跟JSONP的区别以及实战 什么是JSON? 前面简单说了一下,JSON是一种基于文本的数据交换方式,或者叫做数据描述格式,你是否该选用他首先肯定要关注它所拥有的优点. JSON的优点 ...

  10. 动态linq to list排序

    public class QeurySort { public static IList<T> Sort<T>(IList<T> list,string sidx, ...