Documentation
¶
Index ¶
- Constants
- func BuildDateTime() (buildDateTime string)
- func CanReadFromPipe() (bool, error)
- func CheckStringFlag(name string, arg string, required bool) (exists bool, err error)
- func FilepathAbs(inputPath string) (path string, err error)
- func FormatSource(source string) (formattedSource string, err error)
- func FuncCaller() string
- func FuncName() string
- func FuncNameFull() string
- func FuncNameNoParens() string
- func FuncSource() string
- func GoFmtProgramString(goProgramString string) (formattedGoProgramString string, err error)
- func GulpFromPipe() (string, error)
- func GulpFromPipeWithTimeout(timeout time.Duration) (input string, err error)
- func IsCommandInstalled(commandName string) (bool, error)
- func PrintCaller()
- func ProgName() string
- func Round(val float64, places int) (rounded float64)
- func WordSize() int
- type StringFlag
Examples ¶
Constants ¶
const ( FlagRequired = true FlagOptional = false )
Utility function to test string flags.
It avoids boilerplate code testing flags.
It can be called and:-
(1) Required flags can trust the existence of an argument.
// Required flag. exists, err := util.CheckStringFlag("r", flags.r, util.FlagRequired) if !exists { fmt.Fprintf(os.Stderr, "%v\n", err) os.Exit(1) }
(2) Optional flags can test exists.
// Optional flag. exists, err := util.CheckStringFlag("o", flags.o, util.FlagOptional) if exists { // Do something with flag. }
Variables ¶
This section is empty.
Functions ¶
func BuildDateTime ¶
func BuildDateTime() (buildDateTime string)
Return a string with the build date/time and (seconds-ago) of the executable and where it is installed.
func CanReadFromPipe ¶
Check to see if this program can read piped input on this machine.
func CheckStringFlag ¶
func FilepathAbs ¶
Handle Cygwin environment.
The problem:
cygwinPath := "/cygdrive/c/mypath/myfile" windowsPath := filepath.Abs(cygwinPath)
returns: "C:/cygdrive/c/mypath/myfile"
It should return: "C:/mypath/myfile"
func FormatSource ¶
func FuncCaller ¶
func FuncCaller() string
func FuncName ¶
func FuncName() string
Short function name with parentheses.
pkgName.funcName
becomes:
funcName()
Example ¶
// Called from inside func ExampleFuncName() fmt.Println(FuncName())
Output: ExampleFuncName()
func FuncNameFull ¶
func FuncNameFull() string
Full path of function source code with line number followed by full package name of function. Like this:
<mydrive>/golang/src/github.com/urban-wombat/util/util_test.go[39] github.com/urban-wombat/util.TestFuncNameFull
func FuncNameNoParens ¶
func FuncNameNoParens() string
Short function name with NO parentheses.
pkgName.funcName
becomes:
funcName
Example ¶
// Called from inside func ExampleFuncNameNoParens() fmt.Println(FuncNameNoParens())
Output: ExampleFuncNameNoParens
func FuncSource ¶
func FuncSource() string
Short source file name and line number. Like this:
util_test.go[39]
func GoFmtProgramString ¶
NOTE: As per egonelbre suggestion (see https://www.reddit.com/r/golang/comments/9umtp2/beta_release_of_flattables_go_flatbuffers/e95iffn/?context=3) gotables and flattables now calls go/format/Source() to format code. github.com/urban-wombat/util/GoFmtProgramString() will be DEPRECATED at some point.
Instead use go.format.Source()
util.FormatSource()
GoFmtProgramString() pipes a Go program file (as a string) through the Go tool gofmt and returns its output.
Use it to tidy up generated Go source code before writing it to file.
On error the input string is returned unchanged, not an empty "" string. This is unusual but we do that here to avoid crunching goProgramString in the calling function if it happens to be called like this:
goProgramString, err = GoFmtProgramString(goProgramString) if err != nil { // goProgramString is unchanged, not crunched } // goProgramString has been formatted by gofmt
Because this function calls out to gofmt in the operating system, the potential for failure is possible on some machines (and hence not testable during development). Hence a more forgiving return of its input string so as to avoid crunching user data.
func GulpFromPipe ¶
Read and return piped input as a string.
Beware: this blocks waiting for stdin.
stdin, err := util.GulpFromPipe()
func GulpFromPipeWithTimeout ¶
Read and return piped input as a string.
This waits for stdin but only until timeout expires.
stdin, err := util.GulpFromPipe(1 * time.Second)
func IsCommandInstalled ¶
Check whether commandName is installed on this machine.
func PrintCaller ¶
func PrintCaller()
See 2: http://moazzam-khan.com/blog/golang-get-the-function-callers-name
This is a blend of both (above URLs) examples. Provides:-
(1) The function name called.
(2) The function name of the caller.
(2) The file name[line number] at the call.
This is intentionally a print-only function because calling it from another function (other than the one being tracked) will change the calling information by nesting to an additional level.
Types ¶
type StringFlag ¶
type StringFlag struct {
// contains filtered or unexported fields
}
StringFlag implements the flag.Value interface https://golang.org/pkg/flag/#Value
type Value interface { String() string Set(string) error }
func (*StringFlag) AllOk ¶
func (sf *StringFlag) AllOk() bool
AllOk() is specific to gotables.Util It means:-
(1) flag exists (2) flag is set (3) error is nil
func (*StringFlag) Exists ¶
func (sf *StringFlag) Exists() bool
Exists() is specific to gotables.Util
func (*StringFlag) Print ¶
func (sf *StringFlag) Print()
Print to stdout StringFlag field values and method results.
func (*StringFlag) Set ¶
func (sf *StringFlag) Set(s string) error
Set() implements part of the flag.Value interface https://golang.org/pkg/flag/#Value
func (*StringFlag) String ¶
func (sf *StringFlag) String() string
String() implements part of the flag.Value interface https://golang.org/pkg/flag/#Value