Documentation ¶
Overview ¶
Package gelpers provides miscellaneous helper functions.
Index ¶
- func IntMatrixTranspose(m [][]int) [][]int
- func IntSliceContains(s []int, v int) bool
- func IntSliceMax(s []int) int
- func IntSliceMin(s []int) int
- func IntSliceSum(s []int) int
- func SnakeToCamel(s string) string
- func SpaceSepToCamel(s string) string
- func StringPtr(str string) *string
- func StringVal(strPtr *string) string
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IntMatrixTranspose ¶ added in v0.2.0
IntMatrixTranspose returns a 2d slice such that input[r][c] = output[c][r]. It expects a non-nil, non-empty 2d slice where each row has the same length.
Example ¶
package main import ( "fmt" "github.com/ecshreve/gelpers" ) func main() { m := [][]int{{1, 2, 3}, {1, 2, 3}, {1, 2, 3}} fmt.Println(gelpers.IntMatrixTranspose(m)) }
Output: [[1 1 1] [2 2 2] [3 3 3]]
func IntSliceContains ¶
IntSliceContains checks if the given slice contains the given value.
Example ¶
package main import ( "fmt" "github.com/ecshreve/gelpers" ) func main() { s := []int{1, 2, 3} fmt.Println(gelpers.IntSliceContains(s, 2)) }
Output: true
func IntSliceMax ¶ added in v0.2.0
IntSliceMax returns the largest value in the given slice of ints, or -1 if the slice is empty or nil.
Example ¶
package main import ( "fmt" "github.com/ecshreve/gelpers" ) func main() { s := []int{1, 2, 3} fmt.Println(gelpers.IntSliceMax(s)) }
Output: 3
func IntSliceMin ¶ added in v0.2.0
IntSliceMin returns the smallest value in the given slice of ints, or -1 if the slice is empty or nil.
Example ¶
package main import ( "fmt" "github.com/ecshreve/gelpers" ) func main() { s := []int{1, 2, 3} fmt.Println(gelpers.IntSliceMin(s)) }
Output: 1
func IntSliceSum ¶ added in v0.2.0
IntSliceSum returns the sum of all items in the given slice of ints.
Example ¶
package main import ( "fmt" "github.com/ecshreve/gelpers" ) func main() { s := []int{1, 2, 3} fmt.Println(gelpers.IntSliceSum(s)) }
Output: 6
func SnakeToCamel ¶
SnakeToCamel converts a snake_case_string to a CamelCaseString. If the input is not valid then it returns the input string.
A string is considered valid if it satisfies the following:
- non-empty
- begins with a letter
- only contains letters, numbers, and the underscores
Example ¶
package main import ( "fmt" "github.com/ecshreve/gelpers" ) func main() { fmt.Println(gelpers.SnakeToCamel("foo_bar_baz")) }
Output: FooBarBaz
func SpaceSepToCamel ¶
SpaceSepToCamel converts a space separated string to a CamelCaseString. If the input is not valid then it returns the input string.
A string is considered valid if it satisfies the following:
- non-empty
- begins with a letter
- only contains letters, numbers, and spaces
Example ¶
package main import ( "fmt" "github.com/ecshreve/gelpers" ) func main() { fmt.Println(gelpers.SpaceSepToCamel("foo bar baz")) }
Output: FooBarBaz
func StringPtr ¶
StringPtr conveniently converts a string literal into a pointer to string.
Example ¶
package main import ( "fmt" "github.com/ecshreve/gelpers" ) func main() { sPtr := gelpers.StringPtr("hello") fmt.Printf("%T", sPtr) }
Output: *string
func StringVal ¶
StringVal conveniently converts a pointer to string to a string literal, if the given pointer is nil it returns an empty string.
Example ¶
package main import ( "fmt" "github.com/ecshreve/gelpers" ) func main() { s := "hello" sPtr := &s sVal := gelpers.StringVal(sPtr) fmt.Printf("%T", sVal) }
Output: string
Example (Second) ¶
package main import ( "fmt" "github.com/ecshreve/gelpers" ) func main() { var sPtr *string sVal := gelpers.StringVal(sPtr) fmt.Println(sVal == "") }
Output: true
Types ¶
This section is empty.