diff options
| author | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2024-06-15 13:58:30 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-06-15 13:58:30 +0300 |
| commit | 77e4b066cb8d506b4bc944ab4eb2d6e4679e2202 (patch) | |
| tree | 1d549eca619ed36cb881e487cb054c4643ac8376 /pkg/writer/writer.go | |
| parent | change project name to sdp.go (diff) | |
| download | sdp.go-77e4b066cb8d506b4bc944ab4eb2d6e4679e2202.tar.gz sdp.go-77e4b066cb8d506b4bc944ab4eb2d6e4679e2202.tar.bz2 sdp.go-77e4b066cb8d506b4bc944ab4eb2d6e4679e2202.zip | |
enable multithreading with goroutines (#20)
Diffstat (limited to 'pkg/writer/writer.go')
| -rw-r--r-- | pkg/writer/writer.go | 50 |
1 files changed, 29 insertions, 21 deletions
diff --git a/pkg/writer/writer.go b/pkg/writer/writer.go index f3af2fc..abfbf3d 100644 --- a/pkg/writer/writer.go +++ b/pkg/writer/writer.go | |||
| @@ -5,43 +5,51 @@ import ( | |||
| 5 | "strings" | 5 | "strings" |
| 6 | ) | 6 | ) |
| 7 | 7 | ||
| 8 | var output strings.Builder | 8 | type Writer struct { |
| 9 | output strings.Builder | ||
| 10 | temp strings.Builder | ||
| 11 | } | ||
| 9 | 12 | ||
| 10 | var temp strings.Builder | 13 | func NewWriter() *Writer { |
| 14 | return &Writer{ | ||
| 15 | output: strings.Builder{}, | ||
| 16 | temp: strings.Builder{}, | ||
| 17 | } | ||
| 18 | } | ||
| 11 | 19 | ||
| 12 | func Append(str string, a ...any) { | 20 | func (w *Writer) Append(str string, a ...any) { |
| 13 | _, err := output.WriteString(fmt.Sprintf(str, a...)) | 21 | _, err := w.output.WriteString(fmt.Sprintf(str, a...)) |
| 14 | if err != nil { | 22 | if err != nil { |
| 15 | output.WriteString(err.Error()) | 23 | w.output.WriteString(err.Error()) |
| 16 | } | 24 | } |
| 17 | } | 25 | } |
| 18 | 26 | ||
| 19 | func AppendLine(str string, a ...any) { | 27 | func (w *Writer) AppendLine(str string, a ...any) { |
| 20 | Append(str, a...) | 28 | w.Append(str, a...) |
| 21 | output.WriteString("\n") | 29 | w.output.WriteString("\n") |
| 22 | } | 30 | } |
| 23 | 31 | ||
| 24 | func GetWriter() strings.Builder { | 32 | func (w *Writer) GetOutputString() string { |
| 25 | return output | 33 | return w.output.String() |
| 26 | } | 34 | } |
| 27 | 35 | ||
| 28 | func TempAppend(str string, a ...any) { | 36 | func (w *Writer) TempAppend(str string, a ...any) { |
| 29 | _, err := temp.WriteString(fmt.Sprintf(str, a...)) | 37 | _, err := w.temp.WriteString(fmt.Sprintf(str, a...)) |
| 30 | if err != nil { | 38 | if err != nil { |
| 31 | temp.WriteString(err.Error()) | 39 | w.temp.WriteString(err.Error()) |
| 32 | } | 40 | } |
| 33 | } | 41 | } |
| 34 | 42 | ||
| 35 | func TempAppendLine(str string, a ...any) { | 43 | func (w *Writer) TempAppendLine(str string, a ...any) { |
| 36 | TempAppend(str, a...) | 44 | w.TempAppend(str, a...) |
| 37 | temp.WriteString("\n") | 45 | w.temp.WriteString("\n") |
| 38 | } | 46 | } |
| 39 | 47 | ||
| 40 | func TempGetString() string { | 48 | func (w *Writer) TempGetString() string { |
| 41 | return temp.String() | 49 | return w.temp.String() |
| 42 | } | 50 | } |
| 43 | 51 | ||
| 44 | func AppendOutputFromTemp() { | 52 | func (w *Writer) AppendOutputFromTemp() { |
| 45 | output.WriteString(temp.String()) | 53 | w.output.WriteString(w.temp.String()) |
| 46 | temp.Reset() | 54 | w.temp.Reset() |
| 47 | } | 55 | } |