go读取指定行的内容

code

package main

import (
    "os"
    "bufio"
    "fmt"
)

func main(){
    fmt.Println(ReadLine(8))
}

func ReadLine(lineNumber int) string{
    file, _ := os.Open("log.txt")
    fileScanner := bufio.NewScanner(file)
    lineCount := 1
    for fileScanner.Scan(){
        if lineCount == lineNumber{
            return fileScanner.Text()
        }
        lineCount++
    }
    defer file.Close()
    return ""
}

引用

https://blog.csdn.net/x356982611/article/details/80382834

你可能感兴趣的:(Golang,Golang学习笔记)