如何在与某些输入相对应的文件中查找和读取行号?我用谷歌搜索了这段代码,但它将文件的全部内容加载到单个数组中,所有行都被索引。没有更简单的方法吗?


func LinesInFile(fileName string) []string {

    f, _ := os.Open(fileName)

    // Create new Scanner.

    scanner := bufio.NewScanner(f)

    result := []string{}

    // Use Scan.

    for scanner.Scan() {

        line := scanner.Text()

        // Append line to result.

        result = append(result, line)

    }

    return result

}