-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriter.go
71 lines (60 loc) · 1.33 KB
/
writer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// SPDX-FileCopyrightText: 2020-2024 caixw
//
// SPDX-License-Identifier: MIT
package errwrap
import (
"fmt"
"io"
)
// Writer 对 io.WrWriter 的简单封闭
type Writer struct {
io.Writer
Err error
}
// WString 写入字符串
func (w *Writer) WString(str string) *Writer {
if w.Err == nil {
_, w.Err = io.WriteString(w, str)
}
return w
}
// WByte 写入单个字节内容
func (w *Writer) WByte(b byte) *Writer {
return w.WBytes([]byte{b})
}
// WBytes 写入字节内容
func (w *Writer) WBytes(data []byte) *Writer {
if w.Err == nil {
_, w.Err = w.Write(data)
}
return w
}
// WRune 写入单个字节内容
func (w *Writer) WRune(r rune) *Writer {
return w.WBytes([]byte(string(r)))
}
// WRunes 写入字节内容
func (w *Writer) WRunes(rs []rune) *Writer {
return w.WBytes([]byte(string(rs)))
}
// Print 相当于 fmt.Fprint(builder, v...)
func (w *Writer) Print(v ...interface{}) *Writer {
if w.Err == nil {
_, w.Err = fmt.Fprint(w, v...)
}
return w
}
// Println 相当于 fmt.Fprintln(builder, v...)
func (w *Writer) Println(v ...interface{}) *Writer {
if w.Err == nil {
_, w.Err = fmt.Fprintln(w, v...)
}
return w
}
// Printf 相当于 fmt.Fprintf(builder, format, v...)
func (w *Writer) Printf(format string, v ...interface{}) *Writer {
if w.Err == nil {
_, w.Err = fmt.Fprintf(w, format, v...)
}
return w
}