Go file operation

Source: Internet
Author: User
Tags readfile
This is a creation in Article, where the information may have evolved or changed.

Reading the contents of a file

Suppose you want to get content that's interesting in a file, but you don't want to get everything. Assume that the contents of the file are as follows:

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/

You just want to get the line content at the beginning of HTTP. The implementation code is as follows:

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"0 {            fmt.Print(line)        }

The complete code looks like this:

 PackageMainImport("Bufio"    "bytes"    "FMT"    "IO"    "OS"    "Strings"    "Unicode")funcReadFile (FilePathstring{file, err: = OS. Open (FilePath)ifErr! =Nil{FMT. PRINTLN (ERR)return}deferFile. 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 '        ifEqual: = Strings. Index (Line,"http"); Equal >=0{FMT. Print (line)}ifErr = = Io. EOF { Break}ifErr! =Nil{FMT. PRINTLN (Err)}}}funcMain () {//File path: /util/art.txtReadFile (".. /util/art.txt ")}

Output Result:

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/

Welcome to my public number.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.