Improve your Go development with GOsc

Introducing GOsc, a helper package inspired by Lodash to be user friendly and easy to use.

Created on September 2017

Improve your Go development with GOsc

Everything has begun when I was looking for a Go helper package like Lodash, because I was working with high level slices and maps and I needed to manage them filtering, reducing and so on. I found some interesting repo on GitHub, but one was no more active, another hasn't what I was searching and others were not complete, so I told myself: ok, let's write one.

The result is pretty nice, I'd like to call it Godash or Golp but the name were taken, so I switched on a simple GOsc which is fast to write - and without sense.

At this time there are 11 methods for slices, 26 for strings and 4 for numbers, but the plan is to write another for slices and two for maps.

The most difficult challenge was to create functions friendly and for this a typed language doesn't help, so a lot of parameters are interface{} and then asserted to correct types.

Here there are some examples.

Slices - Every / All

I think all we know the every/all function: it checks if all items of the given slice satisfy the given function. Pretty simple, but Go doesn't have it, so here we are. Here I preferred to create separated methods for each type (string, int, float) instead of one with interface{}, because then even the user should use interface{} on callback function and this was not friendly.

Of course there's also the Some / Any method.

It has also some alias: EveryString -> AllString, EveryInt -> AllInt, EveryFloat -> AllFloat.

slice1 := []string{"bar", "baz"}
slice2 := []int{0, 2, 5}

fmt.Println(EveryString(slice1, func(s string) bool {
  return strings.HasPrefix(s, "ba")
})) // true
fmt.Println(AllInt(slice2, func(i int) bool {
  return i%2 == 0
})) // false

Slices - Map

How much the hell are they useful? Why Go hasn't them? Don't worry, GOsc will help you!

slice1 := []string{"foo", "bar", "baz"}
slice2 := []int{3, 5}

fmt.Println(MapString(slice1, strings.ToUpper) // [FOO BAR BAZ]
fmt.Println(MapInt(slice2, func(i int) int {
  return i*2
})) // [6 10]

Slices - Index

Why to not include a simple function like this? At this time you have to write your own to find the index of an item in a slice.

slice1 := []string{"foo", "bar", "baz"}
slice2 := []int{3, 5}

fmt.Println(Index(&slice1, "baz")) // 2
fmt.Println(Index(&slice2, 6) // -1

Slices - InSlice

It's always useful to check if an item is into a slice, so... you're welcome!

slice1 := []string{"foo", "bar", "lazy", "dog"}
slice2 := []int{5, -3, 64, 777}

fmt.Println(InSlice("lazy", &slice1)) // frue
fmt.Println(InSlice(55, &slice2)) // false

Strings - Ucfirst

You need to capitalize the first letter of a string? Good luck. Oh wait, there's Go to rescue you. And yes, it has an alias: UpperFirst.

fmt.Println(gosc.UcFirst("foo")) // Foo
fmt.Println(gosc.UpperFirst("소주")) // 소주

Strings - ToBase64

They keep hard also to just encode in base64. Here you go, man.

fmt.Println(gosc.ToBase64("abc〩")) // YWJj44Cp

If you have any suggestion or issue, please let me know on GitHub or here in the comments!


[UPDATED] Create a contenteditable element with Vue.js


Customize the action button of a toast in Ionic Framework