Read text file into string array (and write)

前端 未结 5 850
-上瘾入骨i
-上瘾入骨i 2020-12-07 08:44

The ability to read (and write) a text file into and out of a string array is I believe a fairly common requirement. It is also quite useful when starting with a language re

5条回答
  •  情深已故
    2020-12-07 09:27

    func readToDisplayUsingFile1(f *os.File){
        defer f.Close()
        reader := bufio.NewReader(f)
        contents, _ := ioutil.ReadAll(reader)
        lines := strings.Split(string(contents), '\n')
    }
    

    or

    func readToDisplayUsingFile1(f *os.File){
        defer f.Close()
        slice := make([]string,0)
    
        reader := bufio.NewReader(f)
    
        for{
    
        str, err := reader.ReadString('\n')
        if err == io.EOF{
            break
        }
    
            slice = append(slice, str)
        }
    

提交回复
热议问题