Help us shape the future of Go! Take the 2026 Go Developer Survey

gslice

package
v0.9.22 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 19, 2026 License: Apache-2.0 Imports: 0 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ToMap

func ToMap[T, V any, K comparable](s []T, f func(T) (K, V)) map[K]V

ToMap collects elements of slice to map, both map keys and values are produced by mapping function f.

🚀 EXAMPLE:

type Foo struct {
	ID   int
	Name string
}
mapper := func(f Foo) (int, string) { return f.ID, f.Name }
ToMap([]Foo{}, mapper) ⏩ map[int]string{}
s := []Foo{{1, "one"}, {2, "two"}, {3, "three"}}
ToMap(s, mapper)       ⏩ map[int]string{1: "one", 2: "two", 3: "three"}

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL