Skip to content

Latest commit

 

History

History
87 lines (70 loc) · 1.5 KB

README.md

File metadata and controls

87 lines (70 loc) · 1.5 KB

gfu is go file utils

GFU contains following functions:

GFU

  1. Read file content as slice of strings via ReadAllLines
import (
	"github.com/wissance/gfu"
	// other imports
)

var lines []string
var err error

// some operations ...

lines, err = gfu.ReadAllLines("my_file.txt", false)
if len(lines) > 0 {
	
}
// 
  1. Read file content as a string via ReadAllText:
import (
	"github.com/wissance/gfu"
	// other imports
)

var text string
var err error

// some operations ...

text, err = gfu.ReadAllText("my_file.txt")
  1. Write slice of strings in file via WriteAllLines This Write overwrites existing file, to add some lines without file erasing use AppendAllLines
import (
	"github.com/wissance/gfu"
	// other imports
)

lines := []string{
"{",
"    \"id\": 1,",
"    \"name\": \"Michael Ushakov\"",
"}",
}
file := "write_all_lines_test.txt"
err := gfu.WriteAllLines(file, lines, "\n")
  1. Write text in file via WriteAllText, this is boring wrap around os.WriteFile
import (
	"github.com/wissance/gfu"
	// other imports
)

text := "some text"
var err error

// some operations ...

err = gfu.WriteAllText("my_file.txt", text)
  1. Append lines to existing file AppendAllText, if file doesn't exist this function create it
import (
	"github.com/wissance/gfu"
	// other imports
)

lines := []string{
"{",
"    \"id\": 2,",
"    \"name\": \"Alex Petrov\"",
"}",
}
file := "write_all_lines_test.txt"
err := gfu.AppendAllLines(file, lines, "\n")