go-file-utils is a lightweight Go package providing small, reusable helpers for common file and JSON operations.
It wraps repetitive standard-library file handling while keeping error handling in the caller's control.
Add the package to your project:
go get github.com/danieljmanningdev/go-file-utilsThen import it:
import "github.com/danieljmanningdev/go-file-utils"- Check whether a file exists
- Open files using standard Go error handling
- Read text files line by line
- Write slices of strings as individual lines
- Decode JSON directly into structs, maps, or other Go values
- Write pretty-printed JSON to files
- Uses ordinary
stringpaths with no custom path types - Returns errors to the caller rather than forcing panic-based handling
FileExists returns true when the path exists and points to a file.
package main
import (
"fmt"
gofileutils "github.com/danieljmanningdev/go-file-utils"
)
func main() {
if gofileutils.FileExists("config.json") {
fmt.Println("File exists")
return
}
fmt.Println("File not found")
}OpenFile opens a file for reading and returns the file together with any error encountered.
package main
import (
"log"
gofileutils "github.com/danieljmanningdev/go-file-utils"
)
func main() {
file, err := gofileutils.OpenFile("example.txt")
if err != nil {
log.Fatal(err)
}
defer file.Close()
}ReadLines reads a text file using a scanner and returns each line as an element in a slice.
package main
import (
"fmt"
"log"
gofileutils "github.com/danieljmanningdev/go-file-utils"
)
func main() {
lines, err := gofileutils.ReadLines("notes.txt")
if err != nil {
log.Fatal(err)
}
for i, line := range lines {
fmt.Printf("Line %d: %s\n", i+1, line)
}
}WriteLines creates or overwrites a file and writes each string as a separate line.
package main
import (
"log"
gofileutils "github.com/danieljmanningdev/go-file-utils"
)
func main() {
lines := []string{
"First line",
"Second line",
"Third line",
}
if err := gofileutils.WriteLines("output.txt", lines); err != nil {
log.Fatal(err)
}
}ReadJSON opens a JSON file and decodes its contents directly into a target Go value.
The target should normally be passed as a pointer.
package main
import (
"fmt"
"log"
gofileutils "github.com/danieljmanningdev/go-file-utils"
)
type Config struct {
App string `json:"app"`
Active bool `json:"active"`
}
func main() {
var config Config
if err := gofileutils.ReadJSON("config.json", &config); err != nil {
log.Fatal(err)
}
fmt.Printf("Loaded config: %+v\n", config)
}ReadJSON can also decode into maps or other compatible Go values:
var data map[string]any
if err := gofileutils.ReadJSON("data.json", &data); err != nil {
log.Fatal(err)
}WriteJSON marshals a Go value as indented JSON and writes it to the specified file.
package main
import (
"log"
gofileutils "github.com/danieljmanningdev/go-file-utils"
)
type Config struct {
App string `json:"app"`
Active bool `json:"active"`
}
func main() {
config := Config{
App: "FileUtility",
Active: true,
}
if err := gofileutils.WriteJSON("config.json", config); err != nil {
log.Fatal(err)
}
}func FileExists(path string) bool
func OpenFile(path string) (*os.File, error)
func ReadLines(path string) ([]string, error)
func WriteLines(path string, lines []string) error
func ReadJSON(path string, target any) error
func WriteJSON(path string, value any) errorOperations that can fail return errors to the caller.
This allows applications to decide whether an error should be logged, returned, retried, displayed to a user, or treated as fatal.
lines, err := gofileutils.ReadLines("notes.txt")
if err != nil {
log.Fatal(err)
}The package does not require callers to use panic-based error handling.
The package is intentionally small and grouped by responsibility:
.
├── file.go
├── json.go
├── gofileutils_test.go
├── go.mod
├── LICENSE
└── README.md
file.go contains general file and line-based helpers.
json.go contains JSON encoding and decoding helpers.
go-file-utils is intended to provide a small convenience layer over common Go standard-library file operations.
The package deliberately avoids introducing unnecessary abstractions around file paths or error handling. Functions accept ordinary string paths and return errors where appropriate.
It is suitable for use in command-line tools, web applications, scripts, utilities, and other Go programs that repeatedly perform basic file or JSON operations.
This project is open source under the terms of the MIT License.