이 름 : 바다아이
|
조회수 : 8829
짧은 주소 : https://www.bada-ie.com/su/?661591819751
// Writing files in Go follows similar patterns to the// ones we saw earlier for reading.package main
import (
"bufio""fmt""io/ioutil""os"
)
funccheck(e error) {
if e != nil {
panic(e)
}
}
funcmain() {
// To start, here's how to dump a string (or just// bytes) into a file.
d1 := []byte("hello\ngo\n")
err := ioutil.WriteFile("dat1.txt", d1, 0644)
check(err)
// For more granular writes, open a file for writing.
f, err := os.Create("dat2.txt")
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()
}
결과
wrote 5 bytes
wrote 7 bytes
wrote 9 bytes
$ cat dat1.txt
hello
go
$ cat dat2.txt
some
writes
buffered
출처 : https://gobyexample.com/writing-files
|
|
번 호
카테고리
제 목
이름
조회수
Copyright ⓒ 2001.12. bada-ie.com. All rights reserved.
이 사이트는 리눅스에서 firefox 기준으로 작성되었습니다. 기타 브라우저에서는 다르게 보일 수 있습니다.
[ Ubuntu + GoLang + PostgreSQL + Mariadb ]
서버위치 : 오라클 클라우드 춘천 실행시간 : 0.06946 초 to webmaster... gogo sea. gogo sea.