A collection of handy utility functions for Golang with no external dependencies!
Most of the magic is done via Go's reflect
package allowing you to get around
having to implement the same functions with different signatures to handle
desired types.
Slice based operations:
// Filter out unique elements from a slice
in := []string{"foo", "bar", "baz", "foo"}
out := []string{}
err := godash.Unique(in, &out)
// out == ["foo", "bar", "baz"]
// Determine if two slices are equal by their contents
s1 := []int{1,2,3,4,5}
s2 := []int{5,4,3,2,1}
ok, err := godash.SliceEqual(s1, s2)
// ok, err == <true, nil>
// Determine if an element exists in a slice
s := []string{"foo", "bar", "baz"}
ok, err := godash.Includes(s, "baz")
// ok, err == <true, nil>
// Append to a slice only if the slice does not already contain the elements
in := []string{"foo", "bar", "baz", "foo"}
err := godash.AppendUniq(&in, "foo", "hello", "world")
// in == ["foo", "bar", "baz", "foo", "hello", "world"]
Encoding based operations:
type Person struct {
Name string `json:"name"`
}
p := Person{Name: "John Doe"}
// Encode to pretty JSON (4 space indent)
bytes, err := godash.ToPrettyJSON(p)
// Encode to JSON with no indentation (minified)
bytes, err := godash.ToJSON(p)
Map based operations:
// Get all the keys in a map
m := map[string]int{"foo": 3, "bar": 6}
o := []string{}
err := MapKeys(m, &o)
// o == ["key", "bar"]
// Get all the values in a map
m := map[string]int{"foo": 3, "bar": 6}
o := []int{}
err := MapValues(m, &o)
// o == [3, 6]
Visit godoc for further API documentation as new functions are implemented.
$ make test
- Fork it
- Create your feature branch (
git checkout -b feature/my-new-feature
) - Commit your changes (
git commit -m 'Add some feature'
) - Push to the branch (
git push origin feature/my-new-feature
) - Create a new Pull Request