generics

package
v0.0.0-...-26bcfa9 Latest Latest
Warning

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

Go to latest
Published: May 25, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GMin

func GMin[T constraints.Ordered](x, y T) T

GMin is a generic min(a, b) function. using golang.org/x/exp/constraints package

Actual definition of constraints.Ordered as follows
```
type Ordered interface {
	Integer | Float | ~string
}
```

func JSONUnmarshal

func JSONUnmarshal[T any](data []byte, dst *T) error

JSONUnmarshal は、型パラメータ T を定義し引数を dst *T とすることで、T のポインタを使用することを呼び出し側に強制しています。 従来はこのような引数は interface{} を使って定義されておりどのような型でも渡すことができていたため、 ポインタでない型を渡してしまうことによる実行時エラーの懸念がありました。

func SumFloats

func SumFloats(m map[string]float64) float64

SumFloats adds together the values of m.

func SumInts

func SumInts(m map[string]int64) int64

SumInts adds together the values of m.

func SumIntsOrFloats

func SumIntsOrFloats[K comparable, V int64 | float64](m map[K]V) V

SumIntsOrFloats sums the values of map m. It supports both int64 and float64 as types for map values.

func SumNumbers

func SumNumbers[K comparable, V Number](m map[K]V) V

SumNumbers sums the values of map m. Its supports both integers and floats as map values.

Types

type Number

type Number interface {
	int64 | float64
}

Number type constraint NOTE: type argument で指定している `K comparable` の `comparable` も Go で事前定義されたの type constraint の一つ (the comparable constraint is predeclared in Go) 事前定義された type constraint は現在のところ以下2つのみでシンプルになっている * comparable * any

Jump to

Keyboard shortcuts

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