package module
v0.0.0-...-717f0da Latest Latest

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

Go to latest
Published: May 8, 2021 License: MIT Imports: 12 Imported by: 0


(yet another) fileutil (package)

Small Go library of functions to deal with files:

  • does something exist at this path
  • does this path represent a file
  • does this path represent a directory
  • is this an empty directory
  • etc. (actually right now I think that's about it!)

It's a very thin wrapper around standard library functions that are just slightly cumbersome to deal with or to keep their usage straight after not using them for a while.



Package fileutil wraps standard library functions to get info about files and directories that is otherwise a little convoluted to work out every time it's needed.

In the future the package may add functions to help manipulate files and directories.



This section is empty.


This section is empty.


func BaseNoext

func BaseNoext(name string) string

BaseNoext returns the basename of path (ie, the filename without the preceding path components) without the extension (what *I* sometimes erroneously think of as the "basename").

NB: uses package path, so meant to work on slash-separated paths.

func CopyFile

func CopyFile(src, dst string) error

Stolen from Russ Cox "Error Handling — Problem Overview"

But also cf. CopyFile from Dave Cheney's

func DetectContentType

func DetectContentType(rs io.ReadSeeker) (string, error)

Detect content-type without having the whole file in memory.

comment from reference: At most the first 512 bytes of data are used:


func FindFiles

func FindFiles(base string) ([]string, error)

FindFiles recursively finds (or lists) all files under base. Directories are ignored. It's like `find base type -f`. NOTE: This *will* return "hidden" files (files starting with a dot). I'm not sure if that's desireable, but for now the caller is responsible for filtering those out if they aren't wanted.

func IsDir

func IsDir(p string) bool

IsDir returns true if p is a path to an existing directory.

func IsEmpty

func IsEmpty(p string) bool

IsEmpty returns true if p is a dir and contains no files.

func IsExist

func IsExist(p string) bool

IsExist returns true if p is a path to an existing file or directory.

func IsFile

func IsFile(p string) bool

IsFile returns true if p is a path to an existing regular file.

func MD5

func MD5(path string) (string, error)

func ReadFileLines

func ReadFileLines(p string) ([]string, error)

ReadFileLines is an error-returning version of ReadFileLinesDie. See description of ReadFileLinesDie for details.

func ReadFileLinesDie

func ReadFileLinesDie(p string) []string

ReadFileLinesDie wraps ioutil.ReadFile with the additions of returning the lines of the file p as a string slice, and it takes care of getting rid of the final "" element caused by the final newline in the file. And, it dies if it encounters an error. This is obviously only for situations where that's appropriate, like solving puzzles.

func ReadFileNonBlankNonCommentLines

func ReadFileNonBlankNonCommentLines(p string) ([]string, error)

ReadFileNonBlankNonCommentLines is like the above ReadFileLines functions, but ignores blank lines and lines starting with "#" and "//".

func ReadFileNonBlankNonCommentLinesDie

func ReadFileNonBlankNonCommentLinesDie(p string) []string

ReadFileNonBlankNonCommentLinesDie is a panicing version of ReadFileNonBlankNonCommentLines.


This section is empty.

Jump to

Keyboard shortcuts

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