1

If I have some data to store into last row of a CSV file, how should I do it with Go? I do not know how to insert new data. I searched on google but no result at all.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
user12952781
  • 161
  • 1
  • 2
  • 10
  • You can [open the file with the append option](https://stackoverflow.com/a/45971452), then [create a new `csv.Writer`](https://godoc.org/encoding/csv#NewWriter) on that file and then [`Write`](https://godoc.org/encoding/csv#Writer.Write) your data to it. If that doesn't work feel free to edit your question with your code :) – xarantolus Apr 08 '20 at 08:15
  • Does this answer your question? [Append to a file in Go](https://stackoverflow.com/questions/7151261/append-to-a-file-in-go) – George Lindsell Apr 08 '20 at 08:25
  • yeah, that's work on the other example, but for my program, i encounter a problem which is i want to add the new data into the CS V file which type is string, but only slices acceptable with the append option, how do i do it? – user12952781 Apr 08 '20 at 20:25

2 Answers2

3

If you want simply append some data to your csv file, you can open the file with flag and perm and write new things on the file.

Here is a sample code:

package main

import (
    "encoding/csv"
    "log"
    "os"
)

func main() {
    path := "sample.csv"
    file, err := os.OpenFile(path, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
    if err != nil {
        log.Fatal(err)
    }
    defer file.Close()

    var data [][]string
    data = append(data, []string{"nice", "good"})
    data = append(data, []string{"good", "better"})

    w := csv.NewWriter(file)
    w.WriteAll(data)
}
Ilkyun Im
  • 129
  • 5
  • first argument to append must be slice; have string type string is not an expression // hi , how should i fix this problem, the result is the string that i pass by the parameter – user12952781 Apr 08 '20 at 09:25
  • @user12952781 I think that the error would come from append(). Please check whether you give the first argument ```data```. The new data you want to insert should be second argument: ```data = append(data, new_data)``` – Ilkyun Im Apr 08 '20 at 12:37
  • go:145:15: cannot use result (type string) as type []string in append, if i want to append the data which format is string ? – user12952781 Apr 08 '20 at 13:56
  • i would like to ask your gmail for further information about this,i got the code example for you for understanding what i am talking about, due to my English not that well, maybe you can leave your email here? thanks and very appreciate :) – user12952781 Apr 08 '20 at 14:53
0

This has been answered here:

Append to a file in Go

I have flagged this question as a duplicate.

I like the example given in the go docs here:

package main

import (
    "log"
    "os"
)

func main() {
    // If the file doesn't exist, create it, or append to the file
    f, err := os.OpenFile("access.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
    if err != nil {
        log.Fatal(err)
    }
    if _, err := f.Write([]byte("appended some data\n")); err != nil {
        log.Fatal(err)
    }
    if err := f.Close(); err != nil {
        log.Fatal(err)
    }
}

The flag os.O_APPEND is the important part which will add the new line to the end of the file.

Hope that helps.

  • Hi sir, actually i still don know how to use the "log", do you have another simple answer? And also, i encounter a problem which is i cant append the data to the file which the data is string format. – user12952781 Apr 08 '20 at 09:29
  • The log package is used here to handle errors. If any of the functions fail, the error passed to `log.Fatal()` will print to standard error and end the program. More about that here: https://golang.org/pkg/log/ For appending data to the file in string format, you can just convert the string to byte slice `b := []byte("ABC€")` more info on that here: https://yourbasic.org/golang/convert-string-to-byte-slice/ Essentially strings and byte slices are equivalent in go: "In Go, a string is in effect a read-only slice of bytes." a quote from a good Go blog post https://blog.golang.org/strings – George Lindsell Apr 09 '20 at 11:38