Golang input/output: Difference between revisions
From wikinotes
(→Files) |
(→Files) |
||
Line 49: | Line 49: | ||
<blockquote> | <blockquote> | ||
If the files are small, you can work with them using <code>os</code> | If the files are small, you can work with them using <code>os</code> | ||
== Reading == | |||
<blockquote> | |||
Small Files | |||
<syntaxhighlight lang="go"> | <syntaxhighlight lang="go"> | ||
require "os" | require "os" | ||
// | // read entire file into []byte | ||
conts, err := os.ReadFile("/var/tmp/foo.txt") | conts, err := os.ReadFile("/var/tmp/foo.txt") | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Buffered Reading | |||
<syntaxhighlight lang="go"> | <syntaxhighlight lang="go"> | ||
func check(e error) { | func check(e error) { | ||
Line 79: | Line 80: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Reader interface | |||
</blockquote><!-- Reading --> | |||
== Writing == | |||
<blockquote> | |||
Small Files | |||
<syntaxhighlight lang="go"> | |||
require "os" | |||
// replace file (creating if necessary) | |||
err := os.WriteFile("/var/tmp/foo.txt", []byte("abc"), 0644) | |||
</syntaxhighlight> | |||
Buffered Writing | |||
<syntaxhighlight lang="go"> | <syntaxhighlight lang="go"> | ||
fd, err := os.Create(filepath) // create, or truncate file (0666 perms) | fd, err := os.Create(filepath) // create, or truncate file (0666 perms) | ||
Line 91: | Line 106: | ||
fd.Sync() | fd.Sync() | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Writer interface | |||
</blockquote><!-- Writing --> | |||
</blockquote><!-- Files --> | </blockquote><!-- Files --> | ||
Revision as of 20:08, 18 June 2022
Documentation
fmt
https://pkg.go.dev/fmt@go1.18.2 io/ioutil
(read/write)https://pkg.go.dev/io/ioutil@go1.18.3 textproto
(sockets)https://pkg.go.dev/net/textproto@go1.18.3
Stdin, Stdout, Stderr
fmt.Fprintln(os.Stdout, "writes stdout") fmt.Fprintln(os.Stderr, "writes stderr")
Reader, Writer
// read a file into `[]bytes` until EOF conts, err := os.ReadAll(reader)buf := make([]byte, 64) _, err := os.ReadFull(reader, buf) // fill buffer to capacity, reading from reader switch { case errors.Is(err, os.EOF): // nothing to read (0 bytes) case errors.Is(err, os.ErrUnexpectedEOF): // end of file, before filling buffer }filepath := "/var/tmp/foo.txt" _, err := os.WriteString(writer, "abcd")
Files
If the files are small, you can work with them using
os
Reading
Small Files
require "os" // read entire file into []byte conts, err := os.ReadFile("/var/tmp/foo.txt")Buffered Reading
func check(e error) { if e != nil { panic(e) } } // create or open file for reading filepath = "/var/tmp/foo.txt" fd, err := os.Open(filepath) // open for reading fd, err := os.Create(filepath) // create, or truncate file (0666 perms) check(err) // read up to 5 bytes buf := make([]byte, 5) bytes_r, err := fd.Read(buf) check(err)Reader interface
Writing
Small Files
require "os" // replace file (creating if necessary) err := os.WriteFile("/var/tmp/foo.txt", []byte("abc"), 0644)Buffered Writing
fd, err := os.Create(filepath) // create, or truncate file (0666 perms) check(err) bytes_w, err := fd.Write([]byte("abc")) check(err) bytes_w, err := fd.WriteString("abc") check(err) fd.Sync()Writer interface
Networking
Sockets
net.Dial()
creates sockets of various types.import "net" // unix socketfile conn, err = net.Dial("unix", "/var/tmp/foo.sock") // inet socket conn, err = net.Dial("tcp", "10.10.10.10:6600") defer conn.Close() // sending message to socket _, err = conn.Write([]byte("search title 'it ceases to be'")) reply := make([]byte, 1024) _, err = conn.Read(reply) fmt.Println(string(reply))HTTP