Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>For manipulating files, the <code>os</code> package is your friend:</p> <pre><code>f, err := os.Open("myfile") if err != nil { panic(err) } defer f.Close() </code></pre> <p>For more control over how the file is open, see <code>os.OpenFile()</code> instead <a href="http://golang.org/pkg/os/#OpenFile" rel="noreferrer">(doc)</a>.</p> <p>For reading files, there are many ways. The <code>os.File</code> type returned by <code>os.Open</code> (the <code>f</code> in the above example) implements the <code>io.Reader</code> interface (it has a <code>Read()</code> method with the right signature), it can be used directly to read some data in a buffer (a <code>[]byte</code>) or it can also be wrapped in a buffered reader (type <code>bufio.Reader</code>).</p> <p>Specifically for binary data, the <code>encoding/binary</code> package can be useful, to read a sequence of bytes into some typed structure of data. You can see an example in the <a href="http://golang.org/pkg/encoding/binary/#Read" rel="noreferrer">Go doc here</a>. The <code>binary.Read()</code> function can be used with the file read using the <code>os.Open()</code> function, since as I mentioned, it is a <code>io.Reader</code>.</p> <p>And there's also the simple to use <code>io/ioutil</code> package, that allows you to read the whole file at once in a byte slice (<code>ioutil.ReadFile()</code>, which takes a file name, so you don't even have to open/close the file yourself), or <code>ioutil.ReadAll()</code> which takes a <code>io.Reader</code> and returns a slice of bytes containing the whole file. Here's the <a href="http://golang.org/pkg/io/ioutil/#ReadAll" rel="noreferrer">doc on ioutil</a>.</p> <p>Finally, as others mentioned, you can google about the Go language using "golang" and you should find all you need. The <a href="https://groups.google.com/forum/#!forum/golang-nuts" rel="noreferrer">golang-nuts mailing list</a> is also a great place to look for answers (make sure to search first before posting, a lot of stuff has already been answered). To look for third-party packages, check the <a href="http://godoc.org/" rel="noreferrer">godoc.org</a> website.</p> <p>HTH</p>
 

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