Categorygithub.com/go-corelibs/filewriter
modulepackage
1.0.1
Repository: https://github.com/go-corelibs/filewriter.git
Documentation: pkg.go.dev

# README

godoc codecov Go Report Card

filewriter - file writer that does not keep open file handles

filewriter is a package for writing things to files that other processes may also be writing to at the same time so it does not keep any open file handles longer than absolutely necessary.

Installation

> go get github.com/go-corelibs/filewriter@latest

Examples

Write, WalkFile, Remove

func main() {
    // create a new writer using defaults and a temp file
    var err error
    var fw filewriter.FileWriter
    if fw, err = filewriter.New().Make(); err != nil {
        panic(err)
    }
    defer fw.Remove() // cleanup the file when we're done
    // write things to the file
    if err = fw.Write([]byte("two lines\nof text")); err != nil {
        panic(err)
    }
    // need to read the file? fw.ReadFile gives the whole contents,
    // however for large files perhaps the WalkFile is better!
    stopped := fw.WalkFile(func(line string) (stop bool) {
        // do something with this line of file contents and
        // to stop the walking process, return true, otherwise
        // the walk continues to the end of the file
        return
    })
    // stopped == false because the func did not return true
}

Go-CoreLibs

Go-CoreLibs is a repository of shared code between the Go-Curses and Go-Enjin projects.

License

Copyright 2023 The Go-CoreLibs Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use file except in compliance with the License.
You may obtain a copy of the license at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

# Functions

New constructs a new Builder instance with the DefaultOpenFlag and DefaultFileMode.

# Variables

DefaultFileMode is the file mode used by NewFileWriter.
DefaultOpenFlag is the os.OpenFile flag used by NewFileWriter.
DefaultTempFile is the os.CreateTemp pattern used by NewFileWriter.

# Interfaces

Builder is the buildable interface for constructing new FileWriter instances.
FileWriter is an io.WriteCloser that does not keep file handles open any longer than necessary and provides additional methods for interacting with the underlying file.