Go语言程序:统计文件中的行数
在 Go 编程语言中,文件是用于存储数据的命名资源,可以从中读取或写入数据。它为程序提供了一种持久化数据的方法,即使程序执行完毕后数据仍然存在。本文将讨论几种统计文件行数的方法。
方法 1:使用用户自定义函数
在本方法中,我们将编写一个 Go 语言程序,使用 bufio、fmt 和 os 包来统计文件中存在的行数。
算法
步骤 1 − 首先,我们需要导入 fmt、bufio 和 os 包。
步骤 2 − 现在,开始 main 函数。在 main() 函数内部创建一个新文件并将其存储在一个变量中。
步骤 3 − 如果在此过程中发生任何错误,则将其存储在名为 err 的变量中并在屏幕上打印出来。
步骤 4 − 现在,为了写入此文件,请使用 bufio 包中的 NewWriter() 函数,并使用另一个函数 WriteString() 将字符串值输入到文件中。
步骤 5 − 现在,使用 Open() 函数打开文件,并使用 scan() 函数读取文件中存在的数据。
步骤 6 − 再次使用 for 循环迭代文件中存在的数据,并在每次获取新行时递增 count 变量。
步骤 7 − 迭代完成后,count 变量包含该特定文件中存在的行数。
步骤 8 − 最后,我们需要使用 fmt.Println() 函数在屏幕上打印此结果。
示例 1
在以下示例中,我们将使用用户自定义函数来统计文件中存在的行数。
package main import ( "bufio" "fmt" "os" ) func main() { // creating a new file file, err := os.Create("newFile.txt") if err != nil { fmt.Println(err) return } defer file.Close() // writing data in the file writer := bufio.NewWriter(file) _, err = writer.WriteString("Hello, World!\n this is the first line\n followed by the second line\n and the third one") if err != nil { fmt.Println(err) return } err = writer.Flush() if err != nil { fmt.Println(err) return } // fetching the file file, err = os.Open("newFile.txt") if err != nil { fmt.Println(err) return } defer file.Close() scanner := bufio.NewScanner(file) scanner.Split(bufio.ScanLines) // getting the number of lines var count int for scanner.Scan() { count++ } if err := scanner.Err(); err != nil { fmt.Println(err) return } fmt.Println("Number of lines in file:", count) }
输出
Number of lines in file: 4
方法 2:使用内置函数
在本方法中,我们将编写一个 Go 语言程序,使用内置库函数来统计文件中存在的行数。
语法
func len(v Type) int
len() 函数用于获取任何参数的长度。它将要查找其长度的数据类型变量作为参数,并返回表示变量长度的整数值。
func Split(str, sep string) []string
Split() 函数用于通过提供的分隔符分割字符串。此函数存在于 strings 包中,它接受要分割的字符串以及分隔符作为参数。然后,该函数返回最终的字符串数组作为结果。
算法
步骤 1 − 首先,我们需要导入所需的包。
步骤 2 − 现在,开始 main 函数。在 main() 函数内部创建一个新文件并将其存储在一个变量中。
步骤 3 − 现在,为了写入此文件,请使用函数 WriteString() 并将字符串值输入到文件中。
步骤 4 − 现在,使用 ioutil 包中的 ReadFile() 函数打开文件以读取文件中存在的数据。
步骤 5 − 使用 split() 函数统计行数并在屏幕上打印出来。
示例 1
在本例中,我们将学习如何使用诸如 writestring() 之类的内置函数来统计文件中存在的行数。
package main import ( "bufio" "fmt" "io/ioutil" "os" "strings" ) func main() { // creating a new file file, err := os.Create("newFile.txt") if err != nil { fmt.Println(err) return } defer file.Close() // writing data in the file writer := bufio.NewWriter(file) _, err = writer.WriteString("Hello, World!\n this is the first line\n followed by the second line\n and the third one\n and a new one") if err != nil { fmt.Println(err) return } err = writer.Flush() if err != nil { fmt.Println(err) return } // fetching the file data, err := ioutil.ReadFile("newFile.txt") if err != nil { panic(err) } lines := strings.Split(string(data), "\n") fmt.Printf("Number of lines: %d\n", len(lines)) }
输出
Number of lines: 5
示例 2
在本例中,我们使用了 reader 包中存在的内置库函数 ReadLine(),以便编写一个 Go 语言程序来统计给定文件中的行数。
package main import ( "bufio" "fmt" "os" ) func main() { // creating a new file file, err := os.Create("newFile.txt") if err != nil { fmt.Println(err) return } defer file.Close() // writing data in the file writer := bufio.NewWriter(file) _, err = writer.WriteString("Hello, World!\n this is the first line\n followed by the second line\n and the third one\n and a new one") if err != nil { fmt.Println(err) return } err = writer.Flush() if err != nil { fmt.Println(err) return } file, err = os.Open("newFile.txt") if err != nil { panic(err) } defer file.Close() reader := bufio.NewReader(file) lineCount := 0 for { _, _, err := reader.ReadLine() if err != nil { break } lineCount++ } fmt.Printf("Number of lines: %d\n", lineCount) }
输出
Number of lines: 5
Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.
结论
在第一个示例中,我们使用了用户自定义函数,而在第二个方法中,我们使用了两个内置函数示例来统计文件中存在的行数。