fileutil

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2020 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package fileutil provides helper functions for working with files and directories.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CopyDirContents

func CopyDirContents(src, dst string) (err error)

CopyDirContents recursively copies contents of the src directory to dst or returns an error otherwise.

Example
package main

import (
	"log"

	"astrophena.me/gen/pkg/fileutil"
)

func main() {
	if err := fileutil.CopyDirContents("phryne", "fisher"); err != nil {
		log.Fatal(err)
	}
}
Output:

func CopyFile

func CopyFile(src, dst string) (err error)

CopyFile copies the src file to dst or returns an error otherwise. Any existing file will be overwritten and it will not copy file attributes.

Example
package main

import (
	"log"

	"astrophena.me/gen/pkg/fileutil"
)

func main() {
	if err := fileutil.CopyFile("/etc/hostname", "/tmp/hostname"); err != nil {
		log.Fatal(err)
	}
}
Output:

func Exists

func Exists(path string) (exists bool)

Exists returns true if a file or directory exists and false otherwise.

Example
package main

import (
	"fmt"

	"astrophena.me/gen/pkg/fileutil"
)

func main() {
	if fileutil.Exists("example") {
		fmt.Println("example exists")
	} else {
		fmt.Println("no example!")
	}
}
Output:

func Files

func Files(dir string, exts ...string) (files []string, err error)

Files returns a slice of files in the directory dir recursively, with file extensions exts, or an error. If no file extensions are supplied, all files are returned.

Example
package main

import (
	"fmt"
	"path/filepath"

	"astrophena.me/gen/pkg/fileutil"
)

func main() {
	dir := filepath.Join("testdata/files")
	fmt.Println(fileutil.Files(dir))
}
Output:

func Mkdir

func Mkdir(dir string) (err error)

Mkdir creates the directory, if it do not already exist or returns an error. It also creates parent directories as needed.

Example
package main

import (
	"log"

	"astrophena.me/gen/pkg/fileutil"
)

func main() {
	if err := fileutil.Mkdir("example"); err != nil {
		log.Fatal(err)
	}
}
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

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