Go文件操作
来源:互联网 发布:天猫淘宝评价采集软件 编辑:程序博客网 时间:2024/04/29 15:46
读取文件中的内容
假设你想获取文件中感兴趣的内容,但是,你不希望获取所有内容。假设文件的内容如下所示:
1. 网页视觉设计理论之少些质感,多些版式.http://www.ynetx.com/theory/560-1.html2. 设计理论:http://www.ynetx.com/design/theory/3.Google的新设计理念Material Designhttp://www.infoq.com/cn/news/2014/07/google-material-design-android/
你只想获取http开头的行内容。实现代码如下:
line, err := reader.ReadString('\n') // skip all line starting without line 'http' // if equal := strings.Index(line, "http"); equal < 0 { // fmt.Print(line) // } //alternatively, only print line starting with 'http' if equal := strings.Index(line, "http"); equal >= 0 { fmt.Print(line) }
完整代码如下所示:
package mainimport ( "bufio" "bytes" "fmt" "io" "os" "strings" "unicode")func ReadFile(filePath string) { file, err := os.Open(filePath) if err != nil { fmt.Println(err) return } defer file.Close() reader := bufio.NewReader(file) for { line, err := reader.ReadString('\n') // skip all line starting without line 'http' // if equal := strings.Index(line, "http"); equal < 0 { // fmt.Print(line) // } //alternatively, only print line starting with 'http' if equal := strings.Index(line, "http"); equal >= 0 { fmt.Print(line) } if err == io.EOF { break } if err != nil { fmt.Println(err) } }}func main(){ //文件路径../util/art.txt ReadFile("../util/art.txt")}
输出结果:
http://www.ynetx.com/theory/560-1.htmlhttp://www.ynetx.com/design/theory/http://www.infoq.com/cn/news/2014/07/google-material-design-android/
欢迎加入我的微信公众号
0 0
- go 语言文件操作
- go语言文件操作
- Go文件操作
- go文件操作
- Go文件操作大全
- GO文件操作
- Go语言文件操作
- Go 文件读写操作
- go 文件操作
- go lang 读写文件操作
- 【GO语言】文件操作例子
- [译]Go文件操作大全
- Go语言学习(九)文件操作
- Go语言_Go读写文件操作
- Go语言学习(九)文件操作
- 使用GO操作excel文件并发送email
- Go 文件操作(创建、打开、读、写)
- go语言中对文件和文件夹的基本操作
- FastJson转JSON 引用问题
- Python 异常
- 使用WCF来实现一个ShadowSocks客户端(四)
- AngularJS获取ng-repeat中的单选框
- AngularJs路由:ngRoute
- Go文件操作
- Git 常用命令
- 线程
- IO操作
- fullpage.js
- IOS App压力测试 - Smart_monkey的安装和使用
- android 字符串工具类 格式化日期字符串 拼接数组 判断字符串是否为空 转换时间显示 转换文件大小 获取中英文字符个数
- oracle学习笔记 oracle软件安装准备工作 第二部分 安装前说明
- 面试经验