Search code examples
goiohelper

Is it bad practice to add helpers for IO operations in Go?


I come from a C# background and am used IO methods like File.ReadAllLines and File.WriteAllLines from the System.IO namespace. I was a bit surprised to learn that Go didn't have convenience functions for these IO operations. In an effort to avoid code duplication, I wrote the below helpers. Is there any reason to not do this?

// WriteBytes writes the passed in bytes to the specified file. Before writing,
// if the file already exists, deletes all of its content; otherwise, creates
// the file.
func WriteBytes(filepath string, bytes []byte) (err error) {
    file, err := os.Create(filepath)
    if err != nil {
        return err
    }
    defer closeWithErrorPropagation(file, &err)

    _, err = file.Write(bytes)
    if err != nil {
        return err
    }

    return err
}

// WriteString writes the passed in sting to the specified file. Before writing,
// if the file already exists, deletes all of its content; otherwise, creates
// the file.
func WriteString(filepath string, text string) (err error) {
    file, err := os.Create(filepath)
    if err != nil {
        return err
    }
    defer closeWithErrorPropagation(file, &err)

    _, err = file.WriteString(text)
    if err != nil {
        return err
    }

    return err
}

// WriteLines writes the passed in lines to the specified file. Before writing,
// if the file already exists, deletes all of its content; otherwise, creates
// the file.
func WriteLines(filepath string, lines []string) (err error) {
    file, err := os.Create(filepath)
    if err != nil {
        return err
    }
    defer closeWithErrorPropagation(file, &err)

    for _, line := range lines {
        _, err := file.WriteString(fmt.Sprintln(line))
        if err != nil {
            return err
        }
    }

    return err
}

func closeWithErrorPropagation(c io.Closer, err *error) {
    if closerErr := c.Close(); closerErr != nil && *err == nil { // Only propagate the closer error if there isn't already an earlier error.
        *err = closerErr
    }
}

Solution

  • os.WriteFile can handle the equivalent functionality of WriteBytes and WriteString functions:

    // func WriteBytes(filepath string, bytes []byte) (err error)
    
    err = os.WriteFile("testdata/hello", []byte("Hello, Gophers!"), 0666)
    
    
    // func WriteString(filepath string, text string) (err error)
    
    text := "Hello, Gophers!"
    err = os.WriteFile("testdata/hello", []byte(text), 0666)
    

    and combined with strings.Join can handle WriteLines:

    //func WriteLines(filepath string, lines []string) (err error)
    
    lines := []string{"hello", "gophers!"}
    err = os.WriteFile("testdata/hello", []byte(strings.Join(lines, "\n")), 0666)