How do I read in a large flat file

后端 未结 4 1320

I have a flat file that has 339276 line of text in it for a size of 62.1 MB. I am attempting to read in all the lines, parse them based on some conditions I have and then insert

4条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-05 18:07

    It seems to me this variant of readLines is shorter and faster than suggested peterSO

    func readLines(filename string) (map[int]string, error) {
        lines := make(map[int]string)
    
        data, err := ioutil.ReadFile(filename)
        if err != nil {
            return nil, err
        }
    
        for n, line := range strings.Split(string(data), "\n") {
            lines[n] = line
        }
    
        return lines, nil
    }
    

提交回复
热议问题