Read text file into string array (and write)

前端 未结 5 851
-上瘾入骨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:38

    If the file isn't too large, this can be done with the ioutil.ReadFile and strings.Split functions like so:

    content, err := ioutil.ReadFile(filename)
    if err != nil {
        //Do something
    }
    lines := strings.Split(string(content), "\n")
    

    You can read the documentation on ioutil and strings packages.

提交回复
热议问题