merge

package
v0.193.1 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2024 License: Apache-2.0 Imports: 2 Imported by: 0

README

Package merge

Merge package helps you to merge various types like slice, map and error.

package main

import (
	"errors"
	"go.llib.dev/frameless/pkg/merge"
)

func ExampleSlice() {
	var (
		a       = []int{1, 2, 3}
		b       = []int{7, 8, 9}
		c       = []int{4, 5, 6}
		d []int = nil
	)
	got := merge.Slice(a, b, c, d)
	_ = got // []int{1, 2, 3, 7, 8, 9, 4, 5, 6}
}

func ExampleMap() {
	var (
		a = map[string]int{"a": 1, "b": 2, "c": 3}
		b = map[string]int{"g": 7, "h": 8, "i": 9}
		c = map[string]int{"d": 4, "e": 5, "f": 6}
		d = map[string]int{"a": 42}
	)
	got := merge.Map(a, b, c, d)
	_ = got
	//
	//	map[string]int{
	//		"a": 42, "b": 2, "c": 3,
	//		"g": 7, "h": 8, "i": 9,
	//		"d": 4, "e": 5, "f": 6,
	//	}
}

func ExampleError() {
	var (
		err1 error = errors.New("first error")
		err2 error = errors.New("second error")
		err3 error = nil
	)

	err := merge.Error(err1, err2, err3)
	errors.Is(err, err1) // true
	errors.Is(err, err2) // true
	errors.Is(err, err3) // true
}

Documentation

Overview

Package merge offers simple tools to combine types like slices, maps, or errors.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Error added in v0.171.1

func Error(errs ...error) error

Error will combine all given non nil error values into a single error value. If no valid error is given, nil is returned. If only a single non-nil error value is given, the error value is returned.

Example
package main

import (
	"errors"
	"go.llib.dev/frameless/pkg/merge"
)

func main() {
	var (
		err1 error = errors.New("first error")
		err2 error = errors.New("second error")
		err3 error = nil
	)

	err := merge.Error(err1, err2, err3)
	errors.Is(err, err1) // true
	errors.Is(err, err2) // true
	errors.Is(err, err3) // true
}

func Map

func Map[K comparable, V any](vss ...map[K]V) map[K]V
Example
package main

import (
	"go.llib.dev/frameless/pkg/merge"
)

func main() {
	var (
		a = map[string]int{"a": 1, "b": 2, "c": 3}
		b = map[string]int{"g": 7, "h": 8, "i": 9}
		c = map[string]int{"d": 4, "e": 5, "f": 6}
		d = map[string]int{"a": 42}
	)
	got := merge.Map(a, b, c, d)
	_ = got
	//
	//	map[string]int{
	//		"a": 42, "b": 2, "c": 3,
	//		"g": 7, "h": 8, "i": 9,
	//		"d": 4, "e": 5, "f": 6,
	//	}
}

func Slice

func Slice[T any](vss ...[]T) []T
Example
package main

import (
	"go.llib.dev/frameless/pkg/merge"
)

func main() {
	var (
		a       = []int{1, 2, 3}
		b       = []int{7, 8, 9}
		c       = []int{4, 5, 6}
		d []int = nil
	)
	got := merge.Slice(a, b, c, d)
	_ = got // []int{1, 2, 3, 7, 8, 9, 4, 5, 6}
}

Types

This section is empty.

Jump to

Keyboard shortcuts

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