Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to read txt documents in Go language

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to read txt documents in Go language. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Each line of the document content is a file name information with spaces to distinguish different items.

Ioutil.ReadFile returns the [] bytes slice type, which is converted to string type using string () and split into slices by line with strings.Split ()

The file name information of each line is sliced into spaces and stored in var dataNameSlice [] string.

The file name information of each line is processed into a string string and stored in var dataName [] string. Here, the first three items of information are underlined with the strings.Join () method.

Package main import ("fmt"io/ioutil"strings"os") func main () {fileName: = "file1.txt" / / txt file path data, err_read: = ioutil.ReadFile (fileName) / / read file if err_read! = nil {fmt.Println ("File read failed!") } dataLine: = strings.Split (string (data), "\ n") / / slice the contents of the file as string var dataNameSlice [] [] string / / Slice var dataName [] string / / to store each line of content into a string for i Line: = range (dataLine) {dataNameSlice = append (dataNameSlice, strings.Split (line, "")) / / each line is sliced by spaces dataName = append (dataName, strings.Join (dataNameSlic [I] [: 3]) ("_")) / / the first three items of each line of content slice are concatenated into string dataName [I] = dataName [I] + ".md" / / fmt.Println (dataName [I]) for I, dataNameString: = range (dataName) {if _, err: = os.Stat (dataNameString) Os.IsNotExist (err) {/ / determine whether the file exists os.Create (dataNameString)} else {fmt.Printf ("% d. [% s] File already exist!\ n", I, dataNameString)}}

Add: golang reads files by line

File, err: = os.Open ("app-2019-06-01.log") if err! = nil {log.Fatal (err)} defer file.Close () scanner: = bufio.NewScanner (file) for scanner.Scan () {lineText: = scanner.Text ()}

Entire read

B, err: = ioutil.ReadFile ("app-2019-06-01.log") / / just pass the file name fmt.Print (err) str: = string (b) / / convert content to a 'string'fmt.Println (str) / / print the content as a' string' this article on "how to read txt documents in Go language" ends here. I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report