pair

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2024 License: MIT Imports: 3 Imported by: 0

README

pair

A generic pair type for Go with JSON and SQL serialization support.

Install

go get github.com/josest/pair

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Pair

type Pair[F, S any] struct {
	// contains filtered or unexported fields
}

Pair of two elements of different/same types.

func Of

func Of[F, S any](f F, s S) Pair[F, S]

Of returns a new pair with the given elements.

Example
p1 := Of("first", "second")
fmt.Println(p1)
fmt.Println(p1.First())
fmt.Println(p1.Second())

p2 := Of(42, errors.New("something went wrong"))
fmt.Println(p2)
Output:

("first", "second")
first
second
(42, &errors.errorString{s:"something went wrong"})

func (Pair[F, S]) First

func (p Pair[F, S]) First() F

First returns the first element of the pair.

func (Pair[F, S]) MarshalJSON

func (p Pair[F, S]) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler.

Example
p := Of(42, "hello")
b, err := p.MarshalJSON()
if err != nil {
	fmt.Println(err)
	return
}
fmt.Println(string(b))
Output:

{"key":42,"value":"hello"}

func (*Pair[F, S]) Scan

func (p *Pair[F, S]) Scan(src any) error

Scan implements the sql.Scanner.

func (Pair[F, S]) Second

func (p Pair[F, S]) Second() S

Second returns the second element of the pair.

func (Pair[F, S]) String

func (p Pair[F, S]) String() string

String returns a string representation of the pair.

func (*Pair[F, S]) UnmarshalJSON

func (p *Pair[F, S]) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler.

Example
raw := []byte(`{"key":42,"value":"hello"}`)
var p Pair[int, string]
if err := json.Unmarshal(raw, &p); err != nil {
	panic(err)
}

fmt.Println(p)
Output:

(42, "hello")

func (Pair[F, S]) Value

func (p Pair[F, S]) Value() (driver.Value, error)

Value implements the driver.Valuer.

Jump to

Keyboard shortcuts

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