aboutsummaryrefslogtreecommitdiff
path: root/pkg/csvWriter/csv.go
diff options
context:
space:
mode:
authorleshe4ka46 <alex9102naid1@ya.ru>2025-10-27 20:36:28 +0300
committerleshe4ka46 <alex9102naid1@ya.ru>2025-10-28 13:42:21 +0300
commitbb833561aa74f02970aee13cdc75973b29716491 (patch)
tree0914668e11dbf825979f7419ce1bc78294cd3f7f /pkg/csvWriter/csv.go
parente17a425dfb3382310fb5863f516dacdca9f44956 (diff)
# This is a combination of 2 commits.
# This is the 1st commit message: unmarshal all formats, merge them in the single table, users are truly unique # This is the commit message #2: i
Diffstat (limited to 'pkg/csvWriter/csv.go')
-rw-r--r--pkg/csvWriter/csv.go62
1 files changed, 62 insertions, 0 deletions
diff --git a/pkg/csvWriter/csv.go b/pkg/csvWriter/csv.go
new file mode 100644
index 0000000..8709644
--- /dev/null
+++ b/pkg/csvWriter/csv.go
@@ -0,0 +1,62 @@
+package csvwriter
+
+import (
+ "bytes"
+ "os"
+)
+
+type CsvWriter struct {
+ f *os.File
+ buf bytes.Buffer
+}
+
+func NewCsvWriter(path string) (*CsvWriter, error) {
+ f, err := os.Create(path)
+ if err != nil {
+ return nil, err
+ }
+
+ w := &CsvWriter{
+ f: f,
+ }
+ return w, nil
+}
+
+func (c *CsvWriter) Close() error {
+ c.Sync()
+ return c.f.Close()
+}
+
+func (c *CsvWriter) Write(strs []string) error {
+
+ for i, str := range strs {
+ if i != 0 {
+ _, err := c.buf.Write([]byte(","))
+ if err != nil {
+ return err
+ }
+ }
+ _, err := c.buf.Write([]byte(str))
+ if err != nil {
+ return err
+ }
+ }
+ c.buf.WriteByte('\n')
+
+ if c.buf.Len() > 1*1024*1024 {
+ err := c.Sync()
+ if err != nil {
+ return err
+ }
+ }
+ return nil
+}
+
+func (c *CsvWriter) Sync() error {
+ _, err := c.f.Write(c.buf.Bytes())
+ if err != nil {
+ return err
+ }
+ c.buf.Reset()
+ return nil
+}