Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Let's make a Go 1-compatible list of all the ways to read and write files in Go.</p> <p>Because file API has changed recently and most other answers don't work with Go 1. They also miss <code>bufio</code> which is important IMHO.</p> <p>In the following examples I copy a file by reading from it and writing to the destination file.</p> <p><strong>Start with the basics</strong></p> <pre><code>package main import ( "io" "os" ) func main() { // open input file fi, err := os.Open("input.txt") if err != nil { panic(err) } // close fi on exit and check for its returned error defer func() { if err := fi.Close(); err != nil { panic(err) } }() // open output file fo, err := os.Create("output.txt") if err != nil { panic(err) } // close fo on exit and check for its returned error defer func() { if err := fo.Close(); err != nil { panic(err) } }() // make a buffer to keep chunks that are read buf := make([]byte, 1024) for { // read a chunk n, err := fi.Read(buf) if err != nil &amp;&amp; err != io.EOF { panic(err) } if n == 0 { break } // write a chunk if _, err := fo.Write(buf[:n]); err != nil { panic(err) } } } </code></pre> <p>Here I used <code>os.Open</code> and <code>os.Create</code> which are convenient wrappers around <code>os.OpenFile</code>. We usually don't need to call <code>OpenFile</code> directly.</p> <p>Notice treating EOF. <code>Read</code> tries to fill <code>buf</code> on each call, and returns <code>io.EOF</code> as error if it reaches end of file in doing so. In this case <code>buf</code> will still hold data. Consequent calls to <code>Read</code> returns zero as the number of bytes read and same <code>io.EOF</code> as error. Any other error will lead to a panic.</p> <p><strong>Using <code>bufio</code></strong></p> <pre><code>package main import ( "bufio" "io" "os" ) func main() { // open input file fi, err := os.Open("input.txt") if err != nil { panic(err) } // close fi on exit and check for its returned error defer func() { if err := fi.Close(); err != nil { panic(err) } }() // make a read buffer r := bufio.NewReader(fi) // open output file fo, err := os.Create("output.txt") if err != nil { panic(err) } // close fo on exit and check for its returned error defer func() { if err := fo.Close(); err != nil { panic(err) } }() // make a write buffer w := bufio.NewWriter(fo) // make a buffer to keep chunks that are read buf := make([]byte, 1024) for { // read a chunk n, err := r.Read(buf) if err != nil &amp;&amp; err != io.EOF { panic(err) } if n == 0 { break } // write a chunk if _, err := w.Write(buf[:n]); err != nil { panic(err) } } if err = w.Flush(); err != nil { panic(err) } } </code></pre> <p><code>bufio</code> is just acting as a buffer here, because we don't have much to do with data. In most other situations (specially with text files) <code>bufio</code> is very useful by giving us <a href="http://weekly.golang.org/pkg/bufio/" rel="noreferrer">a nice API</a> for reading and writing easily and flexibly, while it handles buffering behind the scenes.</p> <p><strong>Using <code>ioutil</code></strong></p> <pre><code>package main import ( "io/ioutil" ) func main() { // read the whole file at once b, err := ioutil.ReadFile("input.txt") if err != nil { panic(err) } // write the whole body at once err = ioutil.WriteFile("output.txt", b, 0644) if err != nil { panic(err) } } </code></pre> <p>Easy as pie! But use it only if you're sure you're not dealing with big files.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload