запись файлов
This commit is contained in:
parent
46671879fc
commit
3e4828bcb4
@ -56,7 +56,7 @@ Epoch
|
|||||||
Хеш SHA1 (SHA1 Hashes)
|
Хеш SHA1 (SHA1 Hashes)
|
||||||
Кодирование Base64 (Base64 Encoding)
|
Кодирование Base64 (Base64 Encoding)
|
||||||
Чтение файлов (Reading Files)
|
Чтение файлов (Reading Files)
|
||||||
Writing Files
|
Запись файлов (Writing Files)
|
||||||
Line Filters
|
Line Filters
|
||||||
File Paths
|
File Paths
|
||||||
Directories
|
Directories
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
// Writing files in Go follows similar patterns to the
|
// Запись файло в Go следует тем же шаблонам, что мы
|
||||||
// ones we saw earlier for reading.
|
// видели ранее в главе "`Чтение`".
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
@ -18,41 +18,44 @@ func check(e error) {
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
// To start, here's how to dump a string (or just
|
// В этом примере показано вот как записать строку
|
||||||
// bytes) into a file.
|
// (или только байты) в файл.
|
||||||
d1 := []byte("hello\ngo\n")
|
d1 := []byte("hello\ngo\n")
|
||||||
err := ioutil.WriteFile("/tmp/dat1", d1, 0644)
|
err := ioutil.WriteFile("/tmp/dat1", d1, 0644)
|
||||||
check(err)
|
check(err)
|
||||||
|
|
||||||
// For more granular writes, open a file for writing.
|
// Для более детальной записи откройте файл для записи.
|
||||||
f, err := os.Create("/tmp/dat2")
|
f, err := os.Create("/tmp/dat2")
|
||||||
check(err)
|
check(err)
|
||||||
|
|
||||||
// It's idiomatic to defer a `Close` immediately
|
// Идиоматично откладывать `закрытие` с помощью `defer`'a
|
||||||
// after opening a file.
|
// сразу после открытия файла.
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
// You can `Write` byte slices as you'd expect.
|
// Вы можете `записать` срез байт, как и ожидается.
|
||||||
d2 := []byte{115, 111, 109, 101, 10}
|
d2 := []byte{115, 111, 109, 101, 10}
|
||||||
n2, err := f.Write(d2)
|
n2, err := f.Write(d2)
|
||||||
check(err)
|
check(err)
|
||||||
fmt.Printf("wrote %d bytes\n", n2)
|
fmt.Printf("wrote %d bytes\n", n2)
|
||||||
|
|
||||||
// A `WriteString` is also available.
|
// Запись строки `WriteString` так же доступна.
|
||||||
n3, err := f.WriteString("writes\n")
|
n3, err := f.WriteString("writes\n")
|
||||||
fmt.Printf("wrote %d bytes\n", n3)
|
fmt.Printf("wrote %d bytes\n", n3)
|
||||||
|
|
||||||
// Issue a `Sync` to flush writes to stable storage.
|
// Выполните синхронизацию `Sync` для сброса записей
|
||||||
|
// в стабильное хранилище.
|
||||||
f.Sync()
|
f.Sync()
|
||||||
|
|
||||||
// `bufio` provides buffered writers in addition
|
// `bufio` предоставляет буферизованных `писателей`
|
||||||
// to the buffered readers we saw earlier.
|
// в дополнение к буферизованным `читателям`, которые
|
||||||
|
// мы видели ранее.
|
||||||
w := bufio.NewWriter(f)
|
w := bufio.NewWriter(f)
|
||||||
n4, err := w.WriteString("buffered\n")
|
n4, err := w.WriteString("buffered\n")
|
||||||
fmt.Printf("wrote %d bytes\n", n4)
|
fmt.Printf("wrote %d bytes\n", n4)
|
||||||
|
|
||||||
// Use `Flush` to ensure all buffered operations have
|
// Используйте `Flush`, чтобы убедиться, что все
|
||||||
// been applied to the underlying writer.
|
// буферизованные операции были применены к основному
|
||||||
|
// модулю записи.
|
||||||
w.Flush()
|
w.Flush()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
# Try running the file-writing code.
|
# Пробуем запустить запись в файл.
|
||||||
$ go run writing-files.go
|
$ go run writing-files.go
|
||||||
wrote 5 bytes
|
wrote 5 bytes
|
||||||
wrote 7 bytes
|
wrote 7 bytes
|
||||||
wrote 9 bytes
|
wrote 9 bytes
|
||||||
|
|
||||||
# Then check the contents of the written files.
|
# Потом проверим, что контент появился в файлах.
|
||||||
$ cat /tmp/dat1
|
$ cat /tmp/dat1
|
||||||
hello
|
hello
|
||||||
go
|
go
|
||||||
@ -13,5 +13,6 @@ some
|
|||||||
writes
|
writes
|
||||||
buffered
|
buffered
|
||||||
|
|
||||||
# Next we'll look at applying some of the file I/O ideas
|
# Далее мы рассмотрим применение некоторых идей файлового
|
||||||
# we've just seen to the `stdin` and `stdout` streams.
|
# ввода-вывода, которые мы только что видели, к потокам
|
||||||
|
# `stdin` и `stdout`.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user