publish writing-files
This commit is contained in:
parent
85061bf84a
commit
c074646a1c
@ -54,7 +54,7 @@ URL Parsing
|
|||||||
SHA1 Hashes
|
SHA1 Hashes
|
||||||
Base64 Encoding
|
Base64 Encoding
|
||||||
Reading Files
|
Reading Files
|
||||||
# Writing Files
|
Writing Files
|
||||||
# File Operations
|
# File Operations
|
||||||
Line Filters
|
Line Filters
|
||||||
Command-Line Arguments
|
Command-Line Arguments
|
||||||
|
@ -7,3 +7,5 @@ go
|
|||||||
2 bytes @ 6: go
|
2 bytes @ 6: go
|
||||||
2 bytes @ 6: go
|
2 bytes @ 6: go
|
||||||
5 bytes: hello
|
5 bytes: hello
|
||||||
|
|
||||||
|
# Next we'll look at writing files.
|
||||||
|
@ -1,14 +1,58 @@
|
|||||||
|
// Writing files in Go follows similar patterns to the
|
||||||
|
// ones we saw earlier for reading.
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "os"
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
func main() {
|
func check(e error) {
|
||||||
file, err := os.Create("writing-files.txt")
|
if e != nil {
|
||||||
if err != nil {
|
panic(e)
|
||||||
panic(err)
|
|
||||||
}
|
}
|
||||||
defer file.Close()
|
|
||||||
file.WriteString("contents\n")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// todo: streaming writes
|
func main() {
|
||||||
|
|
||||||
|
// To start, here's how to dump a string (or just
|
||||||
|
// bytes) into a file.
|
||||||
|
d1 := []byte("hello\ngo\n")
|
||||||
|
err := ioutil.WriteFile("/tmp/dat1", d1, 0644)
|
||||||
|
check(err)
|
||||||
|
|
||||||
|
// For more granular writes, open a file for writing.
|
||||||
|
f, err := os.Create("/tmp/dat2")
|
||||||
|
check(err)
|
||||||
|
|
||||||
|
// It's idiomatic to defer a `Close` immediately
|
||||||
|
// after opening a file.
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
// You can `Write` byte slices as you'd expect.
|
||||||
|
d2 := []byte{115, 111, 109, 101, 10}
|
||||||
|
n2, err := f.Write(d2)
|
||||||
|
check(err)
|
||||||
|
fmt.Printf("wrote %d bytes\n", n2)
|
||||||
|
|
||||||
|
// A `WriteString` is also available.
|
||||||
|
n3, err := f.WriteString("writes\n")
|
||||||
|
fmt.Printf("wrote %d bytes\n", n3)
|
||||||
|
|
||||||
|
// Issue a `Sync` to flush writes to stable storage.
|
||||||
|
f.Sync()
|
||||||
|
|
||||||
|
// `bufio` provides buffered writers in addition
|
||||||
|
// to the buffered readers we saw earlier.
|
||||||
|
w := bufio.NewWriter(f)
|
||||||
|
n4, err := w.WriteString("buffered\n")
|
||||||
|
fmt.Printf("wrote %d bytes\n", n4)
|
||||||
|
|
||||||
|
// Use `Flush` to ensure all buffered operations have
|
||||||
|
// been applied to the underlying writer.
|
||||||
|
w.Flush()
|
||||||
|
|
||||||
|
}
|
||||||
|
17
examples/writing-files/writing-files.sh
Normal file
17
examples/writing-files/writing-files.sh
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
# Try running the file-writing code.
|
||||||
|
$ go run writing-files.go
|
||||||
|
wrote 5 bytes
|
||||||
|
wrote 7 bytes
|
||||||
|
wrote 9 bytes
|
||||||
|
|
||||||
|
# Then check the contents of the written files.
|
||||||
|
$ cat /tmp/dat1
|
||||||
|
hello
|
||||||
|
go
|
||||||
|
$ cat /tmp/dat2
|
||||||
|
some
|
||||||
|
writes
|
||||||
|
buffered
|
||||||
|
|
||||||
|
# Next we'll look at applying some of the file I/O ideas
|
||||||
|
# we've just seen to the `stdin` and `stdout` streams.
|
Loading…
x
Reference in New Issue
Block a user