Can not replace the content of a csv file in Go -
i have created csv file (assume "output.csv") using os.openfile
flags, os.create
, os.rdwr
. i'm doing series of operations on file. in every iteration, need rewrite contents of csv file ("output.csv"). code appends csv file.
before each rewrite, truncate file , seek beginning. example:
package main import ( "fmt" "os" ) func main() { if f, err := os.create("test.csv"); err == nil { defer f.close() n := 10; n > 0; n-- { f.truncate(0) // comment or uncomment f.seek(0, 0) // these lines see difference := 0; < n; i++ { f.writestring(fmt.sprintf("%d\n", i)) } } } else { fmt.println(err) } }
Comments
Post a Comment