This is the package with functions from Python/other language, that you are searching for in Go (Golang)

  • By null
  • Last update: Aug 3, 2022
  • Comments: 1

What go-sugaring is?

This is the package with functions from Python/other language, that you are searching for in Go (Golang)

Disclaimer

Most of the functions from this lib became possible to be written because of generics. So, it is make sense to use the library only if your project is ready to use go >=1.18

Installation and usage

  1. go get github.com/waclawthedev/go-sugaring
  2. Call the function. For example:
print(sugar.Contains([]string{"a", "b", "c"}, "a"))
//output: true

Already implemented functions:

  1. sugar.Contains - check if slice contains element
  2. sugar.IndexesOf - get the slice with occurrences (indexes) of element in slice
  3. sugar.Map - Map function. Python analog
  4. sugar.Filter - Filter function. Python analog
  5. sugar.Reduce - Reduce function. Python analog
  6. sugar.Flatten - Transforms matrix to slice
  7. sugar.Min - min element in slice
  8. sugar.Max - max element in slice
  9. sugar.Keys - get keys of map as slice
  10. sugar.Values - get values of map as slice

Download

go-sugaring.zip

Comments(1)

  • 1

    Get rid of unnecessary allocations

    Hi! I just noticed in you code at line 99 and 107 you perform append operations without previous array allocation. I guess you can allocate slice before loop, because you know the length of map. https://github.com/waclawthedev/go-sugaring/blob/60730179bfd9d6e7aadd49e2cac211709e1883f6/sugar.go#L99