Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. package safebuffer
  2.  
  3. import (
  4. "bytes"
  5. "sync"
  6. )
  7.  
  8. // Buffer is a goroutine safe bytes.Buffer
  9. type Buffer struct {
  10. buffer bytes.Buffer
  11. mutex sync.Mutex
  12. }
  13.  
  14. // Write appends the contents of p to the buffer, growing the buffer as needed. It returns
  15. // the number of bytes written.
  16. func (s *Buffer) Write(p []byte) (n int, err error) {
  17. s.mutex.Lock()
  18. defer s.mutex.Unlock()
  19. return s.buffer.Write(p)
  20. }
  21.  
  22. // String returns the contents of the unread portion of the buffer
  23. // as a string. If the Buffer is a nil pointer, it returns "<nil>".
  24. func (s *Buffer) String() string {
  25. s.mutex.Lock()
  26. defer s.mutex.Unlock()
  27. return s.buffer.String()
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement