package synchelper import ( "io" "sync" ) // NewSyncWriter 返回写互斥的 writer func NewSyncWriter(writer io.Writer) io.Writer { return &syncWriter{ writer: writer, lock: sync.Mutex{}, } } type syncWriter struct { writer io.Writer lock sync.Mutex } func (w *syncWriter) Write(p []byte) (int, error) { w.lock.Lock() defer w.lock.Unlock() return w.writer.Write(p) }