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.