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. Lua 自定义函数string.split

    function string.split(str, delimiter)    if str==nil or str=='' or delimiter==nil then        return ...

  2. 转:各种Adapter的用法

    各种Adapter的用法   同样是一个ListView,可以用不同的Adapter让它显示出来,比如说最常用的ArrayAdapter,SimpleAdapter,SimpleCursorAdapt ...

  3. PHP-PHP-FPM的max_children一些误区

    现在nginx + fpm 基本成为主流的配置,其中我们比较关注的是pm.max_chindren的配置 首先,我们关注一个前提设置: pm = static/dynamic, 这个选项是标识fpm子 ...

  4. shiro 自动登录

    1.出现的错误:did not match the expected credentials---密码不匹配,后来自己写密码验证,其实作用不大: 配置 <!-- Shiro权限过滤过滤器定义 - ...

  5. 自己动手制作CSharp编译器

    在你喜欢的位置(如F盘根目录)新建一个文件夹,并命名为“CSharp开发环境”.找到或下载C#编译器组件(csc.exe和cscui.exe),并放在先前建立的文件夹中.该组件的一般位置在C盘的.NE ...

  6. D3的基本设计思路

    学习一项新技术,首先要搞清楚它的基本设计思路,有了这个宏观的技术架构,使用该技术起来,就会得心应手了.否则,就会不知道如何下手,即使看到人家的例子程序,可能也不知其所以然. 下面,就简单的结合自己研究 ...

  7. [2014.01.27]WFsoft.wfLibrary.wfIniFile 1.5

    完全支持.net 2.0编写,对下一代操作系统平稳过渡.     不使用[DllImport("kernel32")]的方式,完全自主的.net 2.0自主解析.    完整支持键 ...

  8. AJAX-创建XMLHttpRequest对象

    AJAX-创建XMLHttpRequest对象 1.XMLHttpRequest是AJAX的基础,所有现在浏览器都支持,用于在后台与服务器交换数据,也就意味着可以在不加载整个页面的情况下对整个页面进  ...

  9. vmware12无法打开内核设备“\\.\Global\vmx86”

    vmware12 无法打开内核设备"\\.\Global\vmx86": 系统找不到指定的文件.你想要在安装 VMware Workstation 前重启吗? 打开vmware12 ...

  10. 【MySQL】drop大表

    利用硬链接和truncate降低drop table对线上环境的影响 众所周知drop table会严重的消耗服务器IO性能,如果被drop的table容量较大,甚至会影响到线上的正常. 首先,我们看 ...