另客网go项目公用的代码库

writer.go 395B

12345678910111213141516171819202122232425
  1. package sse
  2. import "io"
  3. type stringWriter interface {
  4. io.Writer
  5. WriteString(string) (int, error)
  6. }
  7. type stringWrapper struct {
  8. io.Writer
  9. }
  10. func (w stringWrapper) WriteString(str string) (int, error) {
  11. return w.Writer.Write([]byte(str))
  12. }
  13. func checkWriter(writer io.Writer) stringWriter {
  14. if w, ok := writer.(stringWriter); ok {
  15. return w
  16. } else {
  17. return stringWrapper{writer}
  18. }
  19. }