xfile

package
v0.25.15 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 21, 2024 License: Apache-2.0 Imports: 6 Imported by: 4

README

GoKit - xfile

File kits for Golang development.

Installation

go get -u github.com/likexian/gokit

Importing

import (
    "github.com/likexian/gokit/xfile"
)

Documentation

Visit the docs on GoDoc

Example

check file is exists
exists := xfile.Exists("/data/dev/gokit/LICENSE")
if exists {
    fmt.Println("file is exists")
} else {
    fmt.Println("file not exists")
}
get file size
size, err := xfile.Size("/data/dev/gokit/LICENSE")
if err != nil {
    panic(err)
} else {
    fmt.Println("file size is", size)
}
write text to file
err := xfile.WriteText("/tmp/not-exists-dir/LICENSE", "Copyright 2012-2024 Li Kexian\n")
if err != nil {
    panic(err)
} else {
    fmt.Println("write to file successful")
}

License

Copyright 2012-2024 Li Kexian

Licensed under the Apache License 2.0

Donation

If this project is helpful, please share it with friends.

If you want to thank me, you can give me a cup of coffee.

Documentation

Index

Constants

View Source
const (
	// TypeAll list dir and file
	TypeAll int = iota
	// TypeDir list only dir
	TypeDir
	// TypeFile list only file
	TypeFile
)

Variables

View Source
var (
	// ErrNotExists file is exists error
	ErrNotExists = errors.New("xfile: file is not exists")
	// ErrHasExists file is exists error
	ErrHasExists = errors.New("xfile: the file is exists")
)

Functions

func Append

func Append(fpath string, data []byte) (err error)

Append appends bytes data to file

func AppendText added in v0.25.5

func AppendText(fpath, text string) (err error)

AppendText appends string data to file

func Author

func Author() string

Author returns package author

func Chmod

func Chmod(fpath string, mode os.FileMode) error

Chmod chmods to path without recursion

func ChmodAll

func ChmodAll(root string, mode os.FileMode) error

ChmodAll chmods to path and children, returns the first error it encounters

func Chown

func Chown(fpath string, uid, gid int) error

Chown chowns to path without recursion

func ChownAll

func ChownAll(root string, uid, gid int) error

ChownAll chowns to path and children, returns the first error it encounters

func Copy

func Copy(src, dst string) error

Copy copys file and folder from src to dst

func Exists

func Exists(fpath string) bool

Exists returns path is exists, symbolic link will check the target

func IsDir

func IsDir(fpath string) bool

IsDir returns path is a dir, symbolic link will check the target

func IsFile

func IsFile(fpath string) bool

IsFile returns path is a file, symbolic link will check the target

func IsSymlink(fpath string) bool

IsSymlink returns path is a symbolic link

func Lexists

func Lexists(fpath string) bool

Lexists returns path is exists, symbolic link will not follow

func License

func License() string

License returns package license

func MTime

func MTime(fpath string) (int64, error)

MTime returns the file mtime of path, symbolic link will check the target

func New

func New(fpath string) (*os.File, error)

New opens a file for new and return fd

func NewFile added in v0.25.5

func NewFile(fpath string, isAppend bool) (*os.File, error)

NewFile opens a file and return fd

func Read

func Read(fpath string) ([]byte, error)

Read returns bytes of file

func ReadFirstLine

func ReadFirstLine(fpath string) (line string, err error)

ReadFirstLine returns first NOT empty line

func ReadLastLine added in v0.25.6

func ReadLastLine(fpath string) (line string, err error)

ReadLastLine returns last NOT empty line

func ReadLines

func ReadLines(fpath string, n int) (lines []string, err error)

ReadLines returns N lines of file

func ReadText

func ReadText(fpath string) (string, error)

ReadText returns text of file

func Size

func Size(fpath string) (int64, error)

Size returns the file size of path, symbolic link will check the target

func Version

func Version() string

Version returns package version

func Write

func Write(fpath string, data []byte) (err error)

Write writes bytes data to file

func WriteText

func WriteText(fpath, text string) (err error)

WriteText writes string data to file

Types

type LsFile

type LsFile struct {
	Type int
	Path string
	Name string
}

LsFile is list file info

func ListDir

func ListDir(fpath string, ftype, n int) (ls []LsFile, err error)

ListDir lists dir without recursion

func ListDirAll

func ListDirAll(fpath string, ftype, n int) (ls []LsFile, err error)

ListDirAll lists dir and children, filter by type, returns up to n

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL