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 ...
随机推荐
- cf 732c
/* cf732c 错过的最少次数 _________________________________________________________________________________ ...
- 百度网盘kbengine - warring项目下载地址
http://pan.baidu.com/s/1k4J4y 下载解压之后,请看<<使用说明.doc>>,有更新指导
- Controller
http://www.cnblogs.com/artech/archive/2012/08/15/action-result-03.html
- python logging bydate
#!/usr/bin/env python #_*_coding:utf-8_*_ # vim : set expandtab ts=4 sw=4 sts=4 tw=100 : import logg ...
- node express 304 avoid
method 1 Why do I need this? The right answer is: I don’t need that trick! The example below is just ...
- [转] Linux下 config/configure/Configure、make 、make test/make check、sudo make install 的作用
转自:http://blog.csdn.net/secondjanuary/article/details/8985795 这些都是典型的使用GNU的AUTOCONF和AUTOMAKE产生的程序的安装 ...
- C++程序设计(关于函数中数组传递的一点心得)
题目: 10个学生考完期末考试评卷完成后,老师需要划出及格线,要求如下: (1) 及格线是10的倍数: (2) 保证至少有60%的学生及格: (3) 如果所有的学生都高于60分,则及格线为60分: ...
- STM32 硬件I2C 到底是不是个坑?
/** ****************************************************************************** * @author Maox ...
- 【python】global
#!/usr/bin/python # Filename: func_global.py def func(): global x print 'x is', x x = 2 print 'Chang ...
- PHP 模拟POST请求
/** * 模拟post进行url请求 * @param string $url * @param array $post */ function request_post($url, $post = ...