考虑这个工作示例代码,它扩展(不完全)
type multiWriter struct {
writers []Writer
}
来自io
包,只添加两个名为Remove
和Append
的方法来操作内部切片writers
:
package main
import (
"io"
"os"
)
func main() {
w1, e := os.Create("file1.txt")
if e != nil {
panic(e)
}
w2, e := os.Create("file2.txt")
if e != nil {
panic(e)
}
mw := MultiWriter(w1, w2)
data := []byte("Hello ")
_, e = mw.Write(data)
if e != nil {
panic(e)
}
var m *multiWriter = mw.(*multiWriter)
m.Remove(w2)
w2.Close()
w3, e := os.Create("file3.txt")
if e != nil {
panic(e)
}
m.Append(w3)
data = []byte("World ")
_, e = mw.Write(data)
if e != nil {
panic(e)
}
w3.Close()
w1.Close()
}
func (t *multiWriter) Remove(writers ...io.Writer) {
for i := len(t.writers) - 1; i > 0; i-- {
for _, v := range writers {
if t.writers[i] == v {
t.writers = append(t.writers[:i], t.writers[i+1:]...)
break
}
}
}
}
func (t *multiWriter) Append(writers ...io.Writer) {
t.writers = append(t.writers, writers...)
}
type multiWriter struct {
writers []io.Writer
}
func (t *multiWriter) Write(p []byte) (n int, err error) {
for _, w := range t.writers {
n, err = w.Write(p)
if err != nil {
return
}
if n != len(p) {
err = io.ErrShortWrite
return
}
}
return len(p), nil
}
var _ stringWriter = (*multiWriter)(nil)
func (t *multiWriter) WriteString(s string) (n int, err error) {
var p []byte // lazily initialized if/when needed
for _, w := range t.writers {
if sw, ok := w.(stringWriter); ok {
n, err = sw.WriteString(s)
} else {
if p == nil {
p = []byte(s)
}
n, err = w.Write(p)
}
if err != nil {
return
}
if n != len(s) {
err = io.ErrShortWrite
return
}
}
return len(s), nil
}
// MultiWriter creates a writer that duplicates its writes to all the
// provided writers, similar to the Unix tee(1) command.
func MultiWriter(writers ...io.Writer) io.Writer {
w := make([]io.Writer, len(writers))
copy(w, writers)
return &multiWriter{w}
}
// stringWriter is the interface that wraps the WriteString method.
type stringWriter interface {
WriteString(s string) (n int, err error)
}
有没有简洁的方法,只需要将两个名为Remove
和Append
的方法添加到io.MultiWriter
?
答案 0 :(得分:1)
您无法在其他包中定义类型的方法。代码只能定义类型在同一个包中的方法。
T
表示的类型称为接收器基本类型;它不能是指针或接口类型,必须在与方法相同的包中声明。
因此,没有其他方法可以使用除复制其完整代码之外的方法扩展未导出类型io.multiWriter
,并将方法添加到您自己的类型中。
注意:作为实施说明,一旦找到可移动编写器,在multiWriter.Remove()
方法中,在重新复制后,您可以“中断”(从内部循环)省略其余部分切片:
// ...
if t.writers[i] == v {
t.writers = append(t.writers[:i], t.writers[i+1:]...)
break
}
// ...