files

package
v0.0.0-...-82e7740 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2019 License: MIT Imports: 12 Imported by: 9

README

util-files GoDoc

file library for Go

Installation

go get gopkg.in/goyy/goyy.v0/util/files

Usage

IsExist

fmt.Println(files.IsExist("./example.txt"))
fmt.Println(files.IsExist("./README"))
// Output: 
// true
// false

Read

s, _ := files.Read("./example.txt")
fmt.Println(s)
// Output: Hello world!

Write

filename := "./example.txt"
data := "Hello goyy!"
if err := files.Write(filename, data, 0744); err != nil {
	log.Fatalf("Write %s: %v", filename, err)
}
s, _ := files.Read(filename)
fmt.Println(s)
files.Write(filename, "Hello world!", 0744) // recover
// Output: Hello goyy!

Documentation

Overview

Package files implements file utility functions.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abs

func Abs(path string) (string, error)

Abs returns an absolute representation of path. If the path is not absolute it will be joined with the current working directory to turn it into an absolute path. The absolute path name for a given file is not guaranteed to be unique. Abs calls Clean on the result.

func AbsDir

func AbsDir(file string) (string, error)

Abs returns an absolute representation of path. If the path is not absolute it will be joined with the current working directory to turn it into an absolute path. The absolute path name for a given file is not guaranteed to be unique. Abs calls Clean on the result.

func Copy

func Copy(dstfile string, srcfile string, perm os.FileMode) error

Copy copies from src to dst until either EOF is reached on src or an error occurs.

func Create

func Create(name string) (*os.File, error)

Create creates the named file mode 0666 (before umask), truncating it if it already exists. If successful, methods on the returned File can be used for I/O; the associated file descriptor has mode O_RDWR. If there is an error, it will be of type *PathError.

func CurrentDir

func CurrentDir() (string, error)

func Dir

func Dir(file string) string

Dir returns all but the last element of path, typically the path's directory. After dropping the final element, Dir calls Clean on the path and trailing slashes are removed. If the path is empty, Dir returns ".". If the path consists entirely of separators, Dir returns a single separator. The returned path does not end in a separator unless it is the root directory.

func Extension

func Extension(fileName string) string

Extension returns the <a href="http://en.wikipedia.org/wiki/Filename_extension">file extension</a> for the given file name, or the empty string if the file has no extension. The result does not include the '{@code .}'.

func IsExist

func IsExist(filename string) bool

IsExist reports whether the specified file exists. Returns true if the file exists, false if it does not exist.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/files"
)

func main() {
	fmt.Println(files.IsExist("./example.txt"))
	fmt.Println(files.IsExist("./README"))

}
Output:

true
false

func Join

func Join(elem ...string) string

Join joins any number of path elements into a single path, adding a Separator if necessary. Join calls Clean on the result; in particular, all empty strings are ignored. On Windows, the result is a UNC path if and only if the first path element is a UNC path.

func LookPath

func LookPath(file string) (string, error)

LookPath searches for an executable binary named file in the directories named by the PATH environment variable. If file contains a slash, it is tried directly and the PATH is not consulted. The result may be an absolute path or a path relative to the current directory.

func Mkdir

func Mkdir(name string, perm os.FileMode) error

Mkdir creates a new directory with the specified name and permission bits. If there is an error, it will be of type *PathError.

func MkdirAll

func MkdirAll(name string, perm os.FileMode) error

MkdirAll creates a directory named path, along with any necessary parents, and returns nil, or else returns an error. The permission bits perm are used for all directories that MkdirAll creates. If path is already a directory, MkdirAll does nothing and returns nil.

func ModTime

func ModTime(file string) (out time.Time, err error)

ModTime returns the file modification time

func ModTimeUnix

func ModTimeUnix(file string) (int64, error)

ModTimeUnix returns the file modification time

func Read

func Read(filename string) (string, error)

Read reads the file named by filename and returns the contents. A successful call returns err == nil, not err == EOF. Because Read reads the whole file, it does not treat an EOF from Read as an error to be reported.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/files"
)

func main() {
	s, _ := files.Read("./example.txt")
	fmt.Println(s)

}
Output:

Hello world!

func Remove

func Remove(name string) error

Remove removes the named file or directory. If there is an error, it will be of type *PathError.

func Rename

func Rename(oldname, newname string) error

Rename renames (moves) oldpath to newpath. If newpath already exists, Rename replaces it. OS-specific restrictions may apply when oldpath and newpath are in different directories. If there is an error, it will be of type *LinkError.

func SetPriority

func SetPriority(value int)

SetPriority set the priority of the logger.

func Size

func Size(file string) (int64, error)

Size returns the length in bytes for regular files

func Unzip

func Unzip(zipFile, destDir string) error

Unzip decompresses and unarchives a ZIP archive.

func Upload

func Upload(w http.ResponseWriter, r *http.Request, field, confdir, filedir string) (out string, err error)

Upload uploads a file. eg. confdir : /assets filedir : /upl field : art/library out : /assets/upl/art/library/8826e07b8d2111e6ab7854e873b1560c.png

func Uploads

func Uploads(w http.ResponseWriter, r *http.Request, field, confdir, filedir string) (out []string, err error)

Uploads uploads a multipart file. eg. confdir : /assets filedir : /upl field : art/library out : /assets/upl/art/library/8826e07b8d2111e6ab7854e873b1560c.png

func Write

func Write(filename string, data string, perm os.FileMode) error

Write writes data to a file named by filename. If the file does not exist, Write creates it with permissions perm; otherwise Write truncates it before writing.

Example
package main

import (
	"fmt"
	"log"

	"gopkg.in/goyy/goyy.v0/util/files"
)

func main() {
	filename := "./example.txt"
	data := "Hello goyy!"
	if err := files.Write(filename, data, 0755); err != nil {
		log.Fatalf("Write %s: %v", filename, err)
	}
	s, _ := files.Read(filename)
	fmt.Println(s)
	files.Write(filename, "Hello world!", 0755) // recover

}
Output:

Hello goyy!

func Zip

func Zip(files []*os.File, dest string) error

Zip compresses and archives a files.

Types

This section is empty.

Jump to

Keyboard shortcuts

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