package logjson import ( "io" "sync" ) // NewSyncWriter 返回写互斥的 io.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) (n int, err error) { w.lock.Lock() defer w.lock.Unlock() return w.writer.Write(p) }