Jzala
Jzala

Reputation: 43

Trying to test write file from goroutines in Go

Well, part of my code was working without a method approach, I'm trying to test append text to a file and reading from goroutines, but I'm stuck here trying to write it.

What is wrong? the file is created, but I can't append text to it, maybe something obvious, but seems I'm blind, maybe I'm failing understanding some language concepts...

package main

import (
    "bufio"
    "fmt"
    "os"
    "sync"
    "time"
)

var w sync.WaitGroup

type Buffer struct {
    F *os.File
}

func (buff *Buffer) Open(pathName string) (err error) {
    buff.F, err = os.OpenFile(pathName, os.O_APPEND|os.O_CREATE, 0666)
    if err != nil {
        return
    }
    fmt.Println("Open() ok")
    return nil
}
func (buff *Buffer) Close() (err error) {
    err = buff.F.Close()
    if err != nil {
        return
    }
    fmt.Println("Close() ok")
    return nil
}
func (buff *Buffer) Push(data string) (err error) {
    w := bufio.NewWriter(buff.F)
    _, err = fmt.Fprintf(w, "data=%s", data)
    if err != nil {
        return
    }
    w.Flush()
    fmt.Println("Push() ok")
    return nil
}
func checkErr(err error) {
    if err != nil {
        fmt.Println(err.Error())
    }
}
func worker() {
    var err error
    buffer := new(Buffer)
    err = buffer.Open("test")
    checkErr(err)
    err = buffer.Push("data\n")
    checkErr(err)
    time.Sleep(5 * time.Second)
    err = buffer.Close()
    checkErr(err)
    w.Done()
}
func main() {
    w.Add(2)
    go worker()
    go worker()
    w.Wait()
}

Thanks

Upvotes: 0

Views: 1484

Answers (1)

Simon Fox
Simon Fox

Reputation: 6425

Open the file like this:

buff.F, err = os.OpenFile(pathName, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)

The write flag is required to write to the file.

You missed the write error because the return from bufio Flush is ignored. Change Push to:

func (buff *Buffer) Push(data string) (err error) {
    w := bufio.NewWriter(buff.F)
    _, err = fmt.Fprintf(w, "data=%s", data)
    if err != nil {
        return
    }
    err = w.Flush()
    if err != nil {
        return err
    }
    fmt.Println("Push() ok")
    return nil
}

To cleanly append data without intermixing with other pushes, the data must be written with a single call to the file Write method. Use a bytes.Buffer instead of a bufio.Writer to ensure a single call to the file Write method:

func (buff *Buffer) Push(data string) (err error) {
    var b bytes.Buffer
    _, err = fmt.Fprintf(&b, "data=%s", data)
    if err != nil {
        return
    }
    _, err := buff.F.Write(b.Bytes())
    if err != nil {
        return err
    }
    fmt.Println("Push() ok")
    return nil
}

Upvotes: 3

Related Questions