π Io β
import (
"encoding/json"
"os"
"github.com/thuongtruong109/gouse"
)
1. Create path β
Description: Create a path where file to be created.
Input params: (path string)
func CreatePath() {
relativePath := "tmp/example.txt"
if err := gouse.CreatePath(relativePath); err != nil {
gouse.Println("Error creating file:", err)
return
}
println("File created successfully.")
}
2. Read path β
Description: Read the path where file to be read.
Input params: (path string)
func ReadPath() {
relativePath := "tmp/example.txt"
content, err := gouse.ReadPath(relativePath)
if err != nil {
gouse.Println("Error reading file:", err)
return
}
gouse.Println("File content:", string(content))
}
3. Write path β
Description: Write content to the path where file to be updated.
Input params: (path string, content []byte)
func WritePath() {
relativePath := "tmp/example.txt"
newContent := []byte("This is a new content")
if err := gouse.WritePath(relativePath, newContent); err != nil {
gouse.Println("Error writing to file:", err)
return
}
println("File updated successfully.")
}
4. Create dir β
Description: Create folder.
Input params: (folderName string)
func CreateDir() {
err2 := gouse.CreateDir("tmp")
if err2 != nil {
println(err2.Error())
}
println("dir created")
}
5. Current dir β
Description: Get current folder.
func CurrentDir() {
data, err := gouse.CurrentDir()
if err != nil {
println(err.Error())
return
}
println(data)
}
6. Hierarchy dir β
Description: Get the folder hierarchy.
Input params: (position string)
func HierarchyDir() {
data, err := gouse.HierarchyDir(".")
if err != nil {
println(err.Error())
return
}
for _, v := range data {
println(v)
}
}
7. Check dir β
Description: Check if the folder exists.
Input params: (dirName string)
func CheckDir() {
isExist, err1 := gouse.IsExistDir("tmp")
if err1 != nil {
println(err1.Error())
}
if isExist {
println("dir exist")
} else {
println("dir not exist")
}
}
8. Ls dir β
Description: List all files in the folder.
Input params: (position string)
func LsDir() {
data, err := gouse.LsDir(".")
if err != nil {
println(err.Error())
return
}
for _, v := range data {
println(v)
}
}
9. Remove dir β
Description: Remove folder.
Input params: (dirName string)
func RemoveDir() {
err3 := gouse.RemoveDir("tmp")
if err3 != nil {
println(err3.Error())
}
println("dir removed")
}
10. Append to file β
Description: Append data to the file.
Input params: (fileName string, data []string)
func AppendToFile() {
err := gouse.AppendFile("data.json", []string{"this is data 3", "this is data 4"})
if err != nil {
println(err.Error())
}
println("file appended")
}
11. Clean file β
Description: Clean the file.
Input params: (fileName string)
func CleanFile() {
err := gouse.CleanFile("data.json")
if err != nil {
println(err.Error())
}
// or using truncate with 0 bytes
// err := helper.TruncateFile("data.json", 0)
// if err != nil {
// println(err.Error())
// }
println("file cleaned")
}
12. Copy file β
Description: Copy content from one file to another.
Input params: (sourceName string, destinationName string)
func CopyFile() {
err := gouse.CopyFile("data.json", "data2.json")
if err != nil {
println(err.Error())
}
println("file copied")
}
13. Create file β
Description: Create a file.
Input params: (fileName string)
func CreateFile() {
err := gouse.CreateFile("data.json")
if err != nil {
println(err.Error())
}
println("file created")
}
14. File info β
Description: Get file info.
Input params: (fileName string)
func FileInfo() {
data, err := gouse.FileInfo("main.go")
if err != nil {
println(err.Error())
}
gouse.Printf("File info: %+v\n", data.All)
gouse.Println("File info (with name):", data.Name)
gouse.Printf("File info (with size): %d bytes\n", data.Size)
gouse.Println("File info (with permissions):", data.Mode)
gouse.Println("File info (with last modified):", data.ModTime)
gouse.Println("File info (with directory check): ", data.IsDir)
gouse.Printf("File info (with system process): %+v\n", data.Sys)
}
15. Check file β
Description: Check if the file exists.
Input params: (fileName string)
func CheckFile() {
isExist, err := gouse.IsExistFile("data.json")
if err != nil {
println(err.Error())
}
if isExist {
println("file exist")
} else {
println("file not exist")
}
}
16. File obj β
Description: Write and update object to the file.
Input params: (fileName string, data interface{})
func FileObj() {
type User struct {
Name string
Age int
}
exampleFile := "data.json"
// read file
// data, err := .ReadFileObj[User](exampleFile)
// if err != nil {
// println(err.Error())
// }
// gouse.Printf("data: %+v\n", data)
// // write file
// updateData := append(data, User{
// Name: gouse.Sprintf("name %d", len(data)+1),
// Age: len(data) + 1,
// })
data, err := os.ReadFile(exampleFile)
if err != nil {
println(err.Error())
return
}
gouse.Printf("data: %+v\n", data)
// unmarshal data into a slice of User
var users []User
if err := json.Unmarshal(data, &users); err != nil {
println(err.Error())
return
}
// create a new user
newUser := User{
Name: gouse.Sprintf("name %d", len(users)+1),
Age: len(users) + 1,
}
// append the new user to the slice
users = append(users, newUser)
// marshal the updated slice back to JSON
updatedData, err := json.Marshal(users)
if err != nil {
println(err.Error())
return
}
// write the updated data back to the file
// if err := util.WriteFile(exampleFile, updatedData, 0644); err != nil {
// println(err.Error())
// return
// }
err2 := gouse.WriteFileObj(exampleFile, updatedData)
if err2 != nil {
println(err2.Error())
}
println("data written")
}
17. Read file by line β
Description: Read file by line.
Input params: (fileName string)
func ReadFileByLine() {
data, err := gouse.ReadFileByLine("main.go")
if err != nil {
println(err.Error())
}
for _, v := range data {
println(v)
}
}
18. Remove file β
Description: Remove file.
Input params: (fileName string)
func RemoveFile() {
err := gouse.RemoveFile("data.json")
if err != nil {
println(err.Error())
}
println("file removed")
}
19. Rename file β
Description: Rename file.
Input params: (oldName string, newName string)
func RenameFile() {
err := gouse.RenameFile("data.json", "data2.json")
if err != nil {
println(err.Error())
}
println("file renamed")
}
20. Truncate file β
Description: Truncate data of file.
Input params: (fileName string, size int64)
func TruncateFile() {
err := gouse.TruncateFile("data.json", 10)
if err != nil {
println(err.Error())
}
println("file truncated to 10 bytes")
}
21. Write to file β
Description: Write data to file.
Input params: (fileName string, data []string)
func WriteToFile() {
err := gouse.WriteFile("data.json", []string{"this is data 1", "this is data 2"})
if err != nil {
println(err.Error())
}
println("file written")
}
22. Zip β
Description: Zip files.
Input params: (zipFileName string, filesToZip []string)
func Zip() {
filesToZip := []string{"file1.txt", "file2.txt"}
zipFileName := "archive.zip"
err := gouse.Zip(zipFileName, filesToZip)
if err != nil {
gouse.Println("Error zipping files:", err)
}
println("Files zipped successfully:", zipFileName)
}
23. Unzip β
Description: Unzip files.
Input params: (zipFileName string, destFolder string)
func Unzip() {
destFolder := "unzipped"
zipFileName := "archive.zip"
err := gouse.Unzip(zipFileName, destFolder)
if err != nil {
gouse.Println("Error unzipping files:", err)
}
println("Files unzipped successfully to:", destFolder)
}