helper

package module
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2026 License: GPL-3.0 Imports: 41 Imported by: 18

README

Defacto2 / helper

Go Reference

Helpers are general system functions used by the Defacto2 website, server software. See the reference documentation for usage and examples.

Documentation

Overview

Package helper has general and shared functions.

Index

Examples

Constants

View Source
const (
	// Eraseline is an ANSI escape control to erase the active line of the terminal.
	Eraseline = "\x1b[2K"
	// Timeout is the HTTP client timeout.
	Timeout = 5 * time.Second
	// UserAgent to send with the HTTP request.
	UserAgent = "Defacto2 website (thanks!)"
)
View Source
const (
	DSStore  = ".DS_Store"       // DSStore is the macOS directory service store file.
	TempBase = "defacto2-server" // TempBase is the base subdirectory for temporary files.
)
View Source
const (
	// WriteWriteRead is the file mode for read and write access.
	// The file owner and group has read and write access, and others have read access.
	WriteWriteRead   fs.FileMode = 0o664 // WriteWriteRead is the file mode for read and write access.
	DirWriteReadRead fs.FileMode = 0o755 // DirWriteReadRead sets directory permissions for read, write, and execute.
)
View Source
const (
	Chrs24 = 24
	Chrs25 = 25
	Chrs29 = 29
)
View Source
const LoggerKey string = "logger"

LoggerKey is unused.

Deprecated: As of release v1.5.

Variables

View Source
var (
	ErrDiffLength = errors.New("files are of different lengths")
	ErrDirPath    = errors.New("directory path is a file")
	ErrExistPath  = errors.New("path ready exists and will not overwrite")
	ErrFileEmpty  = errors.New("file is empty with no content")
	ErrFilePath   = errors.New("file path is a directory")
	ErrKey        = errors.New("could not generate a random session key")
	ErrOSFile     = errors.New("os file is nil")
	ErrNoDir      = errors.New("not a directory")
	ErrRead       = errors.New("could not read files")
)

Functions

func Add1

func Add1(a any) int64

Add1 returns the value of a + 1. The type of a must be a signed integer type or the result is 0.

Example
package main

import (
	"fmt"

	"github.com/Defacto2/helper"
)

func main() {
	n := helper.Add1(2)
	fmt.Println(n)
}
Output:
3

func Alpha09 added in v1.6.3

func Alpha09(b []byte, i, n int) bool

Alpha09 returns true if the slice of bytes exclusively contains. alphanumeric characters. Everything else including punctuation returns false. i is the index position and n is the number of bytes to match.

func ByteCount

func ByteCount(b int64) string

ByteCount formats b as in a compact, human-readable unit of measure.

Example
package main

import (
	"fmt"

	"github.com/Defacto2/helper"
)

func main() {
	fmt.Println(helper.ByteCount(0))
	fmt.Println(helper.ByteCount(1024))
	fmt.Println(helper.ByteCount(1024 * 1024))
}
Output:
0B
1k
1M

func ByteCountFloat

func ByteCountFloat(b int64) string

ByteCountFloat formats b in a human-readable unit of measure. Units measured in gigabytes or larger are returned with 1 decimal place.

Example
package main

import (
	"fmt"

	"github.com/Defacto2/helper"
)

func main() {
	fmt.Println(helper.ByteCountFloat(1024 * 1024 * 1024))
}
Output:
1.1 GB

func Capitalize

func Capitalize(s string) string

Capitalize returns a string with the first letter of the first word capitalized. If the first word is an acronym, it is capitalized as a word.

Example
package main

import (
	"fmt"

	"github.com/Defacto2/helper"
)

func main() {
	fmt.Println(helper.Capitalize("hello world"))
}
Output:
Hello world

func CfUUID

func CfUUID(cfid string) (string, error)

CfUUID formats a 35 character, Coldfusion Universally Unique Identifier. to a standard, 36 character, Universally Unique Identifier.

Example
package main

import (
	"fmt"

	"github.com/Defacto2/helper"
)

func main() {
	const cfid = "00000000-0000-0000-0000000000000000"
	newid, _ := helper.CfUUID(cfid)
	fmt.Printf("uuid %s\ncfid %s\n", newid, cfid)
}
Output:
uuid 00000000-0000-0000-0000-000000000000
cfid 00000000-0000-0000-0000000000000000

func ChrLast

func ChrLast(s string) string

ChrLast returns the last character or rune of the string.

Example
package main

import (
	"fmt"

	"github.com/Defacto2/helper"
)

func main() {
	fmt.Println(helper.ChrLast("hello"))
	fmt.Println(helper.ChrLast("abc\n"))
}
Output:
o
c

func CookieStore

func CookieStore(envKey string) ([]byte, error)

CookieStore generates a key for use with the sessions cookie store middleware. envKey is the value of an imported environment session key. But if it is empty, a 32-bit randomized value is generated that changes on every restart.

The effect of using a randomized key will invalidate all existing sessions on every restart.

func Count

func Count(dir string) (int, error)

Count returns the number of files in the given directory.

Example
package main

import (
	"fmt"
	"path/filepath"

	"github.com/Defacto2/helper"
)

func main() {
	dir, _ := filepath.Abs("testdata")
	count, _ := helper.Count(dir)
	fmt.Printf("%v", count)
}
Output:
3

func CountLines added in v1.7.0

func CountLines(r io.Reader) (int, error)

CountLines counts newline bytes in an io.Reader without line length limits.

func CountStream added in v1.7.0

func CountStream(dir string) (int, error)

CountStream returns the file count using chunked directory method. It is more memory efficient with directories containing many items (10,000+) however, it is slightly slower.

func Day

func Day(i int) bool

Day returns true if the i value can be used as a day time value.

func DeObfuscate

func DeObfuscate(s string) string

DeObfuscate deobfuscates the obfuscated string, or returns the original string.

This function is a port of the deobfuscateParam function programmed in ColdFusion (CFML).

Example
package main

import (
	"fmt"

	"github.com/Defacto2/helper"
)

func main() {
	fmt.Println(helper.DeObfuscate("9b1c6"))
}
Output:
1

func DeleteDupe

func DeleteDupe(s ...string) []string

DeleteDupe removes duplicate strings from a slice. The returned slice is sorted and compacted.

Example
package main

import (
	"fmt"

	"github.com/Defacto2/helper"
)

func main() {
	fmt.Println(helper.DeleteDupe("b", "a", "a"))
}
Output:
[a b]

func DeobfuscateID

func DeobfuscateID(id string) int

DeobfuscateID deobfuscates an obfuscated ID to return the primary key of the record. Returns a 0 if the id is not valid.

func DeobfuscateURL

func DeobfuscateURL(rawURL string) int

DeobfuscateURL deobfuscates an obfuscated record URL to return a record's primary key. A URL can point to a Defacto2 record download or detail page. Returns a 0 if the URL is not valid.

func Determine

func Determine(r io.Reader) encoding.Encoding

Determine returns the encoding of the plain text byte slice, either the charmap.ISO8859_1 or charmap.CodePage437 encoding is returned.

Without false-positives, is difficult to determine the encoding of a text slice without a BOM or other metadata, especially a legacy, 8-bit code page encoding vs UTF-8 encoding. For example, the 👾 (alien monster) emoji in UTF-8 is comprised of the bytes 240, 159, 145, 190, which are all valid CP-437 characters.

"👾"	// [240 159 145 190] unicode.UTF8
"👾"	// [240 159 145 190] charmap.CodePage437
Example
package main

import (
	"fmt"
	"strings"

	"github.com/Defacto2/helper"
)

func main() {
	a := strings.NewReader("hello")
	fmt.Println(helper.Determine(a))
}
Output:
ISO 8859-1

func DetermineChar added in v1.7.0

func DetermineChar(sl *slog.Logger, p []byte) encoding.Encoding

DetermineChar returns the encoding based on the presence of common CP-437 or ISO-8859-1 characters. A nil encoding is returned if no encoding is determined.

func DetermineS deprecated added in v1.5.2

func DetermineS(sl *slog.Logger, r io.Reader) encoding.Encoding

Deprecated: Use DetermineWithLogger instead.

func DetermineSequences added in v1.7.0

func DetermineSequences(sl *slog.Logger, p []byte) encoding.Encoding

DetermineSequences returns the encoding based on the presence of common CP-437 or ISO-8859-1 character sequences.

func DetermineSupplement added in v1.7.0

func DetermineSupplement(sl *slog.Logger, p []byte) encoding.Encoding

DetermineSupplement returns unicode.UTF8 if p contains common UTF-8 block/symbol characters (•, ─, █).

func DetermineWithLogger added in v1.7.0

func DetermineWithLogger(sl *slog.Logger, r io.Reader) encoding.Encoding

DetermineWithLogger functions the same as Determine, however you can provide a slog logger to track character or sequence matches for false-positive discoveries and other possible problems.

func Digits added in v1.6.3

func Digits(b []byte, i, n int) bool

Digits returns true if the slice of bytes is a sequence of digits. i is the index position and n is the number of bytes to match.

func DiskStat added in v1.6.5

func DiskStat(path string) (
	total float64, free float64, percentage float64, formatted string, err error,
)

DiskStat returns the total bytes, free bytes, percentage free, and formatted percentage string. If no path is provided, the drive of the current working directory is used.

func DiskUsage

func DiskUsage(path string) (int64, error)

DiskUsage returns the total size of the files in the given directory.

Example
package main

import (
	"fmt"
	"path/filepath"

	"github.com/Defacto2/helper"
)

func main() {
	dir, _ := filepath.Abs("testdata")
	du, _ := helper.DiskUsage(dir)
	fmt.Printf("%v", du)
}
Output:
755105

func Duplicate

func Duplicate(oldpath, newpath string) (int64, error)

Duplicate is a workaround for renaming files across different devices. A cross device can also be a different file system such as a Docker volume. It returns the number of bytes written to the new file. The function returns an error if the newpath already exists.

func DuplicateOW

func DuplicateOW(oldpath, newpath string) (int64, error)

DuplicateOW is a workaround for renaming files across different devices. A cross device can also be a different file system such as a Docker volume. It returns the number of bytes written to the new file. The function will truncate and overwrite the newpath if it already exists.

func Duplicater added in v1.4.0

func Duplicater(r *os.Root, name, newname string) (int64, error)

Duplicater copies the contents of the named file to a new named file with the root. The function returns an error if the newpath already exists.

func DuplicaterOW added in v1.4.0

func DuplicaterOW(r *os.Root, name, newname string) (int64, error)

DuplicaterOW copies the contents of the named file to a new file with the root. The function will truncate and overwrite the newpath if it already exists.

func File

func File(name string) bool

File returns true if the named file exists on the system.

func FileMatch

func FileMatch(name1, name2 string) (bool, error)

FileMatch returns true if the two named files are the same. It returns false if the files are of different lengths or if an error occurs while reading the files. The read buffer size is 4096 bytes.

func FileMatchR added in v1.4.0

func FileMatchR(r *os.Root, name1, name2 string) (bool, error)

FileMatchR returns true if the two named files are the same. It returns false if the files are of different lengths or if an error occurs while reading the files. The read buffer size is 4096 bytes.

func Files

func Files(dir string) ([]string, error)

Files returns the filenames in the given directory.

Example
package main

import (
	"fmt"
	"path/filepath"

	"github.com/Defacto2/helper"
)

func main() {
	dir, _ := filepath.Abs("testdata")
	files, _ := helper.Files(dir)
	fmt.Printf("%v", files)
}
Output:
[PKZ80A1.TXT TEST.BMP TEST.DOC]

func Finds

func Finds(name string, names ...string) bool

Finds returns true if the name is found in the collection of names.

Example
package main

import (
	"fmt"

	"github.com/Defacto2/helper"
)

func main() {
	fmt.Println(helper.Finds("bravo", "alfa", "bravo", "charlie", "delta"))
	fmt.Println(helper.Finds("bravo", "alfa", "charlie", "delta"))
}
Output:
true
false

func FmtSlice

func FmtSlice(s string) string

FmtSlice formats a comma separated string.

Example
package main

import (
	"fmt"

	"github.com/Defacto2/helper"
)

func main() {
	fmt.Println(helper.FmtSlice("alfa,bravo,charlie"))
}
Output:
Alfa, Bravo, Charlie

func IndexTerm added in v1.6.3

func IndexTerm(i int, p []byte) int

IndexTerm searches the p byte array for a predefined list of words and combinations. Any matches will return the location index of the match. If no matches are found a 0 is returned.

The predefined list are items that can trigger online bots.

func Integrity

func Integrity(name string, fsys fs.FS) (string, error)

Integrity returns the sha384 hash of the named embed file. This is intended to be used for Subresource Integrity (SRI) verification with integrity attributes in HTML script and link tags.

func IntegrityBytes

func IntegrityBytes(b []byte) string

IntegrityBytes returns the sha384 hash of the given byte slice.

Example
package main

import (
	"fmt"

	"github.com/Defacto2/helper"
)

func main() {
	fmt.Println(helper.IntegrityBytes([]byte("hello")))
}
Output:
sha384-WeF0h3dEjGnea4ANejO7+5/xtGPkQ1TDVTvNucZm+pASWjx5+QOXvfX2oT3oKGhP

func IntegrityFile

func IntegrityFile(name string) (string, error)

IntegrityFile returns the sha384 hash of the named file. This can be used as a link cache buster.

func IntegrityReader added in v1.7.0

func IntegrityReader(r io.Reader) (string, error)

IntegrityReader calculates the sha384 hash of an io.Reader stream.

func Latency

func Latency() *time.Time

Latency returns the stored, current local time.

func Lines

func Lines(name string) (int, error)

Lines returns the number of lines in the named file.

Example
package main

import (
	"fmt"
	"path/filepath"

	"github.com/Defacto2/helper"
)

func main() {
	name, _ := filepath.Abs(filepath.Join("testdata", "PKZ80A1.TXT"))
	lines, _ := helper.Lines(name)
	fmt.Printf("%v", lines)
}
Output:
175

func LocalHostPing

func LocalHostPing(ctx context.Context, uri, proto string, port int) (int, int64, error)

LocalHostPing sends a HTTP GET request to the provided URI on localhost.

func LocalHosts

func LocalHosts() ([]string, error)

LocalHosts returns a list of local hostnames.

func LocalIPs

func LocalIPs() ([]net.IP, error)

LocalIPs returns a list of local IP addresses.

credit, gosamples

func Logger deprecated

func Logger(_ context.Context) any

Deprecated: As of release v1.5, this function returns nil.

func Mask added in v1.6.0

func Mask(p ...byte) []byte

Mask runs a performant scan of the bytes and replaces any matching. serials or key sequences with a sequence of 0 characters of the same length.

Currently the following patterns are matched

  • 12345-67890-ABCDE-FGHIJ-LMNOP
  • 1234-5678-ABCD-EFGH-IJKL-MNOP
  • 1234-567890A-BCDEFGH-IJKL
  • 1234 1240000 1234000 0000
  • 1234-5678-ABCD-EFGH-IJKL

In addition telephone numbers matching seven digit (or more) phone numbers will have two digits replaced by $$.

And finally, there's are collection words and combos that will be masked with xxx. For example, any occurrence of the word "password" will be masked as "pxxxxxx".

For example: "Hello world B014-56789A-BCDEFGH-G45X example".

Would be masked with: "Hello world 000000000000000000000000 example".

func MaskTerm added in v1.6.3

func MaskTerm(p ...byte) []byte

MaskTerm replaces a predefined list of words and combinations with a series of x characters. For example, any occurrence of the word "password" will be masked as "pxxxxxx".

MaskTerm should not be used with Mask as it duplicates functionality.

func MaxLineLength

func MaxLineLength(s string) int

MaxLineLength counts the character/rune length of the longest line in a string, that is split strictly by newline (\n).

Example
package main

import (
	"fmt"
	"strings"

	"github.com/Defacto2/helper"
)

func main() {
	s := strings.Repeat("a", 100) + "\n" + strings.Repeat("b", 50)
	fmt.Println(helper.MaxLineLength(s))
}
Output:
100

func MkContent

func MkContent(src string) (string, error)

MkContent returns the destination directory for the extracted archive content. The directory is created if it does not exist. The directory is named after the source file.

func NANP added in v1.6.3

func NANP(i int, p []byte) bool

NANP matches an areacode and a 7 digit number, i.e., 305-555-1234. However, area codes below 200 are not matched, ie 199-555-1234.

func Obfuscate

func Obfuscate(s string) string

Obfuscate obfuscates a numeric string to insecurely hide database primary key values when passed along a URL.

This function is a port of the obfuscateParam function programmed in ColdFusion (CFML).

Example
package main

import (
	"fmt"

	"github.com/Defacto2/helper"
)

func main() {
	fmt.Println(helper.Obfuscate("1"))
	fmt.Println(helper.Obfuscate("abc"))
}
Output:
9b1c6
abc

func ObfuscateID

func ObfuscateID(key int64) string

ObfuscateID obfuscates the primary key of a record as a string that is used as a URL param or path.

func Owner

func Owner() (groups []string, username string, err error)

Owner returns the running user and group of the web application. The function returns the group names and the username of the owner.

func PageCount

func PageCount(sum, limit int) int

PageCount returns the maximum pages possible for the sum of records with a record limit per-page.

Example
package main

import (
	"fmt"

	"github.com/Defacto2/helper"
)

func main() {
	fmt.Println(helper.PageCount(1000, 100))
}
Output:
10

func Phone added in v1.6.3

func Phone(i int, p []byte) bool

Phone matches a 3-4, 7 digit telephone number, i.e., "555-1234".

func PhoneDE added in v1.6.6

func PhoneDE(i int, p []byte) bool

PhoneDE matches a 3-3-3, 9 digit telephone number, i.e., "555-123-456".

func PhoneEuro added in v1.6.6

func PhoneEuro(i int, p []byte) bool

PhoneEuro matches a 2-6, 8 digit telephone number, i.e., "55-123456".

func Ping

func Ping(ctx context.Context, uri string) (int, int64, error)

Ping sends a HTTP GET request to the provided URI, it returns the status code and response size.

func ReaderMatch added in v1.7.0

func ReaderMatch(r1, r2 io.Reader) (bool, error)

ReaderMatch returns true if the content of the two readers are the same. The read buffer size is 4096 bytes.

func Released

func Released(s string) (year, month, day int16)

Released returns a string release date as year, month, day int16 values. The string is expected to be in the format "2024-07-15" or "2024-07" or "2024".

Example
package main

import (
	"fmt"

	"github.com/Defacto2/helper"
)

func main() {
	year, month, day := helper.Released("2024-07-15")
	fmt.Println(year, month, day)
}
Output:
2024 7 15

func RenameCrossDevice

func RenameCrossDevice(oldpath, newpath string) error

RenameCrossDevice copies oldpath to newpath and removes oldpath when crossing file system boundaries. It can be used as a workaround for renaming files across different devices and file systems, such as a mounted Docker volume.

func RenameFile

func RenameFile(oldpath, newpath string) error

RenameFile renames a file from oldpath to newpath. An error is returned if oldpath does not exist, or if oldpath is a directory, or newpath already exists, or the rename fails.

func RenameFileOW

func RenameFileOW(oldpath, newpath string) error

RenameFileOW renames a file from oldpath to newpath. If newpath is an existing directory, it is removed. An error is returned if the oldpath does not exist, the oldpath is a directory, or the rename fails.

func RenameRoot added in v1.4.0

func RenameRoot(r *os.Root, oldname, newname string) error

RenameRoot renames the file oldname to newname. An error is returned if oldname does not exist, or if oldname is a directory, or newname already exists, or the rename fails.

func RenameRootOW added in v1.4.0

func RenameRootOW(r *os.Root, oldname, newname string) error

RenameRootOW renames a file from oldname to newname. An error is returned if the oldname does not exist, the oldname is a directory, or the rename fails.

func ReverseInt deprecated

func ReverseInt(i int) (int, error)

Deprecated: use ReverserInt instead.

The error always returns nil.

func ReverserInt added in v1.7.0

func ReverserInt(n int) int

ReverserInt reverses the digits of n.

Example
package main

import (
	"fmt"

	"github.com/Defacto2/helper"
)

func main() {
	i := helper.ReverserInt(123456)
	fmt.Println(i)
}
Output:
654321

func SearchTerm

func SearchTerm(input string) []string

SearchTerm returns a list of search terms from the input string. The input string is split by commas.

Example
package main

import (
	"fmt"

	"github.com/Defacto2/helper"
)

func main() {
	fmt.Println(helper.SearchTerm("quick,brown,fox"))
}
Output:
[quick brown fox]

func ShortMonth

func ShortMonth(month int) string

ShortMonth takes a month integer and abbreviates it to a three letter English month.

Example
package main

import (
	"fmt"

	"github.com/Defacto2/helper"
)

func main() {
	fmt.Println(helper.ShortMonth(1))
}
Output:
Jan

func Size

func Size(name string) int64

Size returns the size of the named file. If the path name does not exist, is inaccessible or is a directory, it returns -1.

func Slug

func Slug(name string) string

Slug returns a URL friendly string of the named group.

Example
package main

import (
	"fmt"

	"github.com/Defacto2/helper"
)

func main() {
	fmt.Println(helper.Slug("Hello World Homepage!"))
}
Output:
hello-world-homepage

func SortNames added in v1.4.1

func SortNames(sep string, names []string) []string

SortNames sorts the names using the filepath separator, where the root files are preferred.

Usually the sep value is a forward slash (/) or a Windows backslash (\).

func SplitAsSpaces

func SplitAsSpaces(s string) string

SplitAsSpaces splits a string at each capital letter.

Example
package main

import (
	"fmt"

	"github.com/Defacto2/helper"
)

func main() {
	fmt.Println(helper.SplitAsSpaces("TheQuickBrownFox"))
}
Output:
The Quick Brown Fox

func Stat

func Stat(name string) bool

Stat stats the named file or directory to confirm it exists on the system.

func StrongIntegrity

func StrongIntegrity(name string) (string, error)

StrongIntegrity returns the SHA-384 checksum value of the named file.

func StrongIntegrityR added in v1.4.0

func StrongIntegrityR(r *os.Root, name string) (string, error)

StrongIntegrityR returns the SHA-386 checksum value of the named file.

func Sum384 added in v1.7.0

func Sum384(r io.Reader) (string, error)

Sum384 returns the hex-encoded SHA-384 checksum value of an io.Reader stream.

func Sum386 deprecated

func Sum386(f *os.File) (string, error)

Deprecated: Use Sum384 instead.

func TimeDistance

func TimeDistance(from, to time.Time, seconds bool) string

TimeDistance describes the difference between two time values. The seconds parameter determines if the string should handle less than a minute values.

Example
package main

import (
	"fmt"
	"time"

	"github.com/Defacto2/helper"
)

func main() {
	oneMinuteAgo := time.Now().Add(-15 * time.Second)
	fmt.Println(helper.TimeDistance(oneMinuteAgo, time.Now(), true))
	fmt.Println(helper.TimeDistance(oneMinuteAgo, time.Now(), false))

	oneHourAgo := time.Now().Add(-time.Hour)
	fmt.Println(helper.TimeDistance(oneHourAgo, time.Now(), true))

	oneHourAhead := time.Now().Add(time.Hour)
	fmt.Println(helper.TimeDistance(oneHourAgo, oneHourAhead, true))
}
Output:
less than 20 seconds
less than a minute
about 1 hour
about 2 hours

func Titleize

func Titleize(s string) string

Titleize returns a string with the first letter of each word capitalized. If a word is an acronym, it is capitalized as a word.

Example
package main

import (
	"fmt"

	"github.com/Defacto2/helper"
)

func main() {
	fmt.Println(helper.Titleize("hello world"))
}
Output:
Hello World

func TmpDir

func TmpDir() string

TmpDir returns the temporary directory for the server, which is a subdirectory of the system temp directory.

func Touch

func Touch(name string) error

Touch creates a new, empty named file. If the file already exists, an error is returned.

func TouchR added in v1.4.0

func TouchR(r *os.Root, name string) error

TouchR creates a new, empty named file. If the file already exists, an error is returned.

func TouchW

func TouchW(name string, data ...byte) (written int, err error)

TouchW creates a new named file with the given data. If the file already exists, an error is returned.

func TouchWR added in v1.4.0

func TouchWR(r *os.Root, name string, data ...byte) (
	written int, err error,
)

TouchWR creates a new named file with the given data. If the file already exists, an error is returned.

func TrimPunct

func TrimPunct(s string) string

TrimPunct removes any trailing, common punctuation characters from the string.

Example
package main

import (
	"fmt"

	"github.com/Defacto2/helper"
)

func main() {
	fmt.Println(helper.TrimPunct("OMG?!?"))
}
Output:
OMG

func TrimRoundBracket added in v1.6.8

func TrimRoundBracket(s string) string

TrimRoundBracket removes the trailing round brackets and any whitespace.

Example
package main

import (
	"fmt"

	"github.com/Defacto2/helper"
)

func main() {
	fmt.Println(helper.TrimRoundBracket("Hello (world)"))
}
Output:
Hello

func TrimRoundBraket deprecated

func TrimRoundBraket(s string) string

TrimRoundBraket is the deprecated name for TrimRoundBracket.

Deprecated: Use TrimRoundBracket instead.

func TruncFilename

func TruncFilename(w int, name string) string

TruncFilename reduces a filename to the length of w characters. The file extension is always preserved with the truncation.

func UTF8

func UTF8(name string) (bool, error)

UTF8 returns true if the named file is a valid UTF-8 encoded file. The function reads the first 512 bytes of the file to determine the encoding.

func Year

func Year(i int) bool

Year returns true if the i value is between 1970 and (inclusive of) the current year.

func Years

func Years(a, b int16) string

Years returns a formatted string representing a single year, consecutive years, a range of years, or if they are the same, it returns a singular year.

Example
package main

import (
	"fmt"

	"github.com/Defacto2/helper"
)

func main() {
	fmt.Println(helper.Years(1990, 2000))
	fmt.Println(helper.Years(1990, 1991))
	fmt.Println(helper.Years(1990, 1990))
}
Output:
the years 1990 - 2000
the years 1990 and 1991
the year 1990

Types

type Extension

type Extension struct {
	Name  string // Name is the file extension.
	Count int64  // Count is the number of files with the extension.
}

Extension is a file extension with a count of files.

func CountExts

func CountExts(dir string) ([]Extension, error)

CountExts returns the file extensions and the number of files in the given directory.

Example
package main

import (
	"fmt"
	"path/filepath"

	"github.com/Defacto2/helper"
)

func main() {
	dir, _ := filepath.Abs("testdata")
	counts, _ := helper.CountExts(dir)
	fmt.Printf("%v", counts)
}
Output:
[{.bmp 1} {.doc 1} {.txt 1}]

Jump to

Keyboard shortcuts

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