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 ...
随机推荐
- 【引】objective-c,6:Autorelease Pool
参考博客: http://blog.leichunfeng.com/blog/2015/05/31/objective-c-autorelease-pool-implementation-princi ...
- 浅析c#中登录窗体和欢迎窗体关闭的问题
第一次在cnbogs发文章,这次来个很基础的,主要给小白看. 在c#的winform编程中,我们经常会做登录窗体或欢迎窗体,并把他们作为启动窗体. 但是,我们有可能会遇到一些问题. 请看下面的代码: ...
- LeetCode-Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For examp ...
- 基于android平台的出题软件---- 每日30题
本app共编写了3个activity,1.Mainactivity作为主界面.2.surface,用来显示随机出的题.3.showresult,用来打印所有做过的题(含结果),一个类function用 ...
- intelliJ idea debug模式下启动慢的原因
今天在idea上以debug方式启动项目的时候,特别慢,启动半天启动不起来,一直以为是数据库连接的原因,但重启mysql服务后也不好使,特别郁闷... 最后发现原来是之前调试的时候加的断点还在,导致启 ...
- git diff提示filemode发生改变(old mode 100644、new mode 10075)
今天clone代码,git status显示修改了大量文件,git diff提示filemode变化,如下: diff --git a/Android.mk b/Android.mkold mode ...
- TextBlock
一.TextBlock与Lable TextBlock是比Lable更底层的控件如果用TextBlock可以完成的需求就尽量不用Lable可以稍微提升一点性能. 参考文档 二.TextBlock内容过 ...
- 集群工具ansible使用方法
ansible简介 ansible是与puppet.saltstack类似的集群管理工具,其优点是仅需要ssh和Python即可使用,而不像puppet.saltstack那样都需要客户端.与pupp ...
- IOS零碎技术整理(2)-隐藏系统Tabbar
原理就是将tabbar移出显示区 -(void)hideSystemTabBar:(UITabBar*) tabbarcontroller { [UIView beginAnimations:nil ...
- 数据库MySQL基本语法思维导图