簡介
io.Writer 跟 io.Reader 一樣,都是 Interface 類型,功能非常強大,在任何需要寫入數據,處理數據流的地方,我們都應該盡可能使用這兩個類型的對象。
io.Writer 的原型:
type Writer interface {
Write(p []byte) (n int, err error)
}
跟 io.Reader 類似,一個對象只要實現了 Write() 函數,這個對象就自動成為 Writer 類型。
常見 Writer 類型
(1)文件操作
使用 os.Create()
創建文件時,會返回一個 os.File
對象,它是一個 struct,但是由于它實現了 Read() ,Write(),Closer() 等函數,因此它同時也是 Reader, Writer, Closer 等類型。
type File struct {
*file // os specific
}
func (f *File) Write(b []byte) (n int, err error) {
if err := f.checkValid("write"); err != nil {
return 0, err
}
n, e := f.write(b)
if n < 0 {
n = 0
}
if n != len(b) {
err = io.ErrShortWrite
}
epipecheck(f, e)
if e != nil {
err = &PathError{"write", f.name, e}
}
return n, err
}
(2)bytes.Buffer
在 Go 語言中,string 類型是 immutable 的,因此它沒有對應的 Writer,也就是說不存在 strings.NewWriter(s) 這種函數。最好的替代方式就是使用 bytes.Buffer
,因為它既是一個 Reader 也是一個 Writer,我們既可以往里面寫也可以往外讀。我們可以通過 buf.String()
得到 string 類型的數據,也可以通過 buf.Bytes()
拿到 []byte 類型的數據。
下面的例子展示了我們通過 bytes.NewBufferString(s) 函數,先在 buffer 中初始化一段 string,然后往里面 append 另外一段 string。
s := "Hello"
buf := bytes.NewBufferString(s)
s2 := "to be appended"
buf.WriteString(s2) // 或者 fmt.Fprint(buf, s2)
fmt.Println("Final string:", buf.String())
(3)http.ResponseWriter
在使用 Go 語言進行 Web 開發時,http.ResponseWriter
是最基本的類型之一,它本身是一個 Interface 類,原型如下:
type ResponseWriter interface {
Header() Header
Write([]byte) (int, error)
WriteHeader(int)
}
可見,它只申明了需要實現三個函數,由于其要求了 Writer() 函數,包含了 Writer 的要求,因此,任何是符合 ResponserWriter 的類型必然是 Writer 類型。
下面是一個http.ResponseWriter
最常見的使用場景和方法的:
一、直接調用 Write() 寫入一串 []byte
func helloHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.Write([]byte("Hello World"))
return
}
func main() {
http.HandleFunc("/", helloHandler)
http.ListenAndServe(":8080", nil)
}
這種方式跟創建文件時,直接往文件對象中寫入 []byte 是一樣的:
func main() {
f, _:= os.Create("test.txt")
if _, err = f.Write([]byte("Hello world")); err != nil {
log.Fatal(err)
}
f.Close()
}
二、或者 使用 io.WriteStirng() 或者 fmt.Fprintf() 往 Writer 中寫入 string
func helloHandler(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "Hello world!")
fmt.Fprintf(w, "Hello, World!")
}
func main() {
http.HandleFunc("/", helloHandler)
http.ListenAndServe(":8080", nil)
}
(4) hash.Hash
hash 包中申明了 Hash 這個 interface 接口,作為所有 hash 函數的公共接口。它也是一個 Writer,原型如下:
type Hash interface {
// Write (via the embedded io.Writer interface) adds more data to the running hash.
// It never returns an error.
io.Writer
// Sum appends the current hash to b and returns the resulting slice.
// It does not change the underlying hash state.
Sum(b []byte) []byte
// Reset resets the Hash to its initial state.
Reset()
// Size returns the number of bytes Sum will return.
Size() int
// BlockSize returns the hash's underlying block size.
// The Write method must be able to accept any amount
// of data, but it may operate more efficiently if all writes
// are a multiple of the block size.
BlockSize() int
}
使用方式
import (
"crypto/sha1"
)
func main() {
passwordHash := sha1.New()
io.WriteString(passwordHash, combination) // 或者直接 passwordHash.Write(combination)
fmt.Printf("Password Hash : %x \n", passwordHash.Sum(nil))
}
(5) bufio
可以通過 bufio.NewWriter(r)
函數來把原 io.Writer 對象封裝成一個 bufio.Writer
對象,從而進行 buffered 讀寫。
package main
import (
"bufio"
"os"
)
func main() {
f, _ := os.Create("file.txt")
w := bufio.NewWriter(f) // Create a new writer.
w.WriteString("ABC") // Write a string to the file.
w.Flush()
}
(6) gzip 壓縮
package main
import (
"bufio"
"compress/gzip"
"fmt"
"io/ioutil"
"os"
"strings"
)
func main() {
// Open file on disk.
name := "file.txt"
f, _ := os.Open("C:\\programs\\" + name)
// Create a Reader and use ReadAll to get all the bytes from the file.
reader := bufio.NewReader(f)
content, _ := ioutil.ReadAll(reader)
// Replace txt extension with gz extension.
name = strings.Replace(name, ".txt", ".gz", -1)
f, _ = os.Create("C:\\programs\\" + name)
w := gzip.NewWriter(f)
w.Write(content)
w.Close()
}