Skip to content

Package gofile provide some utilities for working with local files in golang.

License

Notifications You must be signed in to change notification settings

sidneycao/gofile

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

83 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gofile

Package gofile provide some utilities for working with local files in golang.

go.dev codecov Go Report Card License: MIT FOSSA Status

Install

  $ go get github.com/sidneycao/gofile

Example:

Here is an example of working with local files, more functions can be found in Documentation.

package main

import (
	"fmt"
	"io"
	"log"

	"github.com/sidneycao/gofile"
)

func main() {
	// Load a local file.
	p, err := gofile.Load("./example-file.txt")
	if err != nil {
		// If the file does not exist, will throw up 'no such file' error.
		//
		// But if the parent exist, this file will be create by p.Open().
		log.Println(err)
	}

	// Open a file.
	err = p.Open()
	if err != nil {
		log.Panic(err)
	}

	// Write to a file.
	wData := []string{"hello\n", "i am\n", "a buff\n"}
	err = p.Write(wData)
	if err != nil {
		log.Panic(err)
	}

	// Read a file.
	rData, err := p.Read()
	if err != nil && err != io.EOF {
		log.Println(err)
	}
	fmt.Println(string(rData))

	// Truncate a file.
	err = p.Truncate(0)
	if err != nil {
		log.Panic(err)
	}

	// Read a file line by line.
	wDataLine := []string{"line1\n", "line2\n", "line3\n"}
	err = p.Write(wDataLine)
	if err != nil {
		log.Panic(err)
	}
	rDataLine, err := p.ReadLines()
	if err != nil && err != io.EOF {
		log.Panic(err)
	}
	fmt.Println(rDataLine)

	// remeber to close.
	p.Close()
}

About

Package gofile provide some utilities for working with local files in golang.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages