Set.New
Overview
New creates a set from items.
If you are coming from Node.js, the closest pattern is new Set(arr).
Method on Set — call it on a value of that type after constructing or receiving one from a constructor.
Signature
go
func New[T comparable](items ...T) Set[T]Compare: Node.js · Standard Go · gox
js
const s = new Set([1, 2, 3]);go
s := make(map[T]struct{})
for _, v := range items { s[v] = struct{}{} }go
import "github.com/sahilkhaire/gox/set"
s := set.New(1, 2, 3)Example
go
import "github.com/sahilkhaire/gox/set"
s := set.New(1, 2, 3)Tips
Obtain a Set value first (see constructors on the package overview), then call New.
Standard library alternative
Use the standard library directly:
go
s := make(map[T]struct{})
for _, v := range items { s[v] = struct{}{} }