utils

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2021 License: GPL-3.0 Imports: 18 Imported by: 5

README

GoDoc Go Report Card REUSE status

go-utils (Go Utilities)

Practical and handy functions that are useful in many Go projects, but which are not part of the standards Go libraries.

Sub package file contains functions that help dealing with files and directories, sub package workerpool supports executing many tasks/calculations concurrently.

Detailed documention is on godoc.org.

Installation

Install go-utils via the go tool by executing

go get gitlab.com/mipimipi/go-utils

License

GNU Public License v3.0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contains

func Contains(a interface{}, e interface{}) bool

Contains checks if the array a contains the element e. inspired by: https://stackoverflow.com/questions/10485743/contains-method-for-a-slice

func ExecQuiet

func ExecQuiet(env []string, name string, args ...string) ([]byte, []byte, error)

ExecQuiet executes a shell command. It receives the environment, the name of the command and its arguments. It returns stdout and stderr as byte array

func ExecVerbose

func ExecVerbose(env []string, name string, args ...string) (err error)

ExecVerbose executes a shell command and prints its stdout and stderr simultaneously. It receives the environment, the command name and its arguments.

func HashUint64

func HashUint64(format string, a ...interface{}) uint64

HashUint64 calculates the FNV hash of the input as uint64

func IPaddr

func IPaddr() (net.IP, error)

IPaddr determines the preferred IP address of the current machine (inspired by https://stackoverflow.com/a/37382208)

func MarshalIndentXML

func MarshalIndentXML(a interface{}, prefix string, indent string) (x []byte, err error)

MarshalIndentXML wraps xml.MarshalIndent() adding the prefix <?xml version="1.0"?>

func MarshalXML

func MarshalXML(a interface{}) (x []byte, err error)

MarshalXML wraps xml.Marshal() adding the prefix <?xml version="1.0"?>

func PrintErrorln

func PrintErrorln(format string, a ...interface{})

PrintErrorln prints an error

func PrintInfoln

func PrintInfoln(format string, a ...interface{})

PrintInfoln prints an info text

func PrintMsgln

func PrintMsgln(format string, a ...interface{})

PrintMsgln prints a message

func PrintPlainln

func PrintPlainln(format string, a ...interface{})

PrintPlainln prints a plain text

func PrintWarnln

func PrintWarnln(format string, a ...interface{})

PrintWarnln prints a warning

func ProgressStr

func ProgressStr(msg string, interval time.Duration) (chan<- struct{}, <-chan struct{})

ProgressStr shows and moves a bar '...' on the command line. It can be used to show that an activity is ongoing. The parameter 'interval' steers the refresh rate (in milli seconds). The text in 'msg' is displayed in form of '...'. The progress bar is stopped by sending an empty struct to the returned channel:

chan <- struct{}{}
close(chan)

func RandomNap

func RandomNap(dur time.Duration)

RandomNap executes a sleep for a random number of 0 to dur milliseconds

func RandomString

func RandomString(n int) string

RandomString generates a random string og length n consisting of characters a-z, A-Z and 0-9.

func SplitDuration

func SplitDuration(d time.Duration) map[time.Duration]time.Duration

SplitDuration disaggregates a duration and returns it splitted into hours, minutes, seconds and nanoseconds

func SplitMulti

func SplitMulti(s string, sep string) []string

SplitMulti slices s into all substrings separated by any character of sep and returns a slice of the substrings between those separators. If s does not contain any character of sep and sep is not empty, SplitMulti returns a slice of length 1 whose only element is s. If sep is empty, SplitMulti splits after each UTF-8 sequence. If both s and sep are empty, SplitMulti returns an empty slice.

func UserOK

func UserOK(s string) bool

UserOK print the message s followed by " (Y/n)?" on stdout and askes the user to press either Y (to continue) or n (to stop). Y is treated as default. I.e. if the user only presses return, that's interpreted as if he has pressed Y.

Types

type SysInfo

type SysInfo struct {
	Nodename   string
	Release    string
	Sysname    string
	Version    string
	Machine    string
	Domainname string
}

SysInfo contains all fields of syscall.Utsname but as type string instead of [65]int8 / [65]uint8

func Uname

func Uname() (SysInfo, error)

Uname retrieves system information via syscall.Uname and converts the resulting struct of [65]int8 values in a struct of strings

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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