Menu

1 / 2

Shipped v2 of go-js-array-methods — JS-style Filter, Map, Reduce for Go slices

DEV Community·Gbubemi Attah·6 months ago
#fJWU9Vj2
#go#javascript#opensource#showdev#array
Reading 0:00
15s threshold

If you've ever switched between JavaScript and Go in the same week, you've felt this. You're three lines into a Go function, you reach for .filter() , and then you remember: right, for loop. Or write a one-off helper for the third time this month.

go-js-array-methods provides Filter, Map, Reduce, Includes, At, Slice, Splice, Push, Pop — 30+ methods named exactly like their JS counterparts. Generics-powered, immutable by default, chainable if you want it.

Repo: github.com/bube054/go-js-array-methods

Quick taste

import "github.com/bube054/go-js-array-methods/v2/array"

nums := [] int { 1 , 2 , 3 , 4 , 5 }

even := array . Filter ( nums , func ( n , _ int , _ [] int ) bool { return n % 2 == 0 }) doubled := array . Map ( even , func ( n , _ int , _ [] int ) int { return n * 2 }) // [4, 8]

Enter fullscreen mode

Exit fullscreen mode

Or chainable, if you prefer that style:

arr := array . Array [ int ]{ 1 , 2 , 3 , 4 , 5 }

result := arr . Filter ( func ( n , _ int , _ [] int ) bool { return n % 2 == 0 }) .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.