generated from datumforge/go-template
-
Notifications
You must be signed in to change notification settings - Fork 5
/
rows.go
40 lines (34 loc) · 1.24 KB
/
rows.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package rows
import (
"fmt"
"os"
"strings"
"text/tabwriter"
)
// Writer is defining an interface called `Writer`
type Writer interface {
Write([]string) error
}
// NewTabRowWriter is a function that creates a new instance of the `TabRowWriter` struct,
// which implements the `Writer` interface. It takes a pointer to a `tabwriter.Writer` as a parameter
// and returns a `Writer` interface.
func NewTabRowWriter(w *tabwriter.Writer) Writer {
if w == nil {
w = tabwriter.NewWriter(os.Stdout, 1, 0, 4, ' ', 0) //nolint:gomnd
}
return &TabRowWriter{*w}
}
// TabRowWriter struct is defining a new struct type called `TabRowWriter`. This struct is
// used to implement the `Writer` interface. It has a single field, `tabwriter.Writer`, which is
// embedded within the struct. This allows the `TabRowWriter` struct to have access to all the methods
// and fields of the embedded `tabwriter.Writer`.
type TabRowWriter struct {
tabwriter.Writer
}
// Write method is implementing the `Write` method of the `Writer`
// interface for the `TabRowWriter` struct. It takes a slice of strings called `record` as a parameter
// and returns an error.
func (w *TabRowWriter) Write(record []string) error {
fmt.Fprintln(&w.Writer, strings.Join(record, "\t"))
return nil
}