generic

package
v0.0.0-...-f750034 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Desensitization

func Desensitization[T AnyString](str T) string

func F

func F()

func Filter

func Filter[T any](f func(T) bool, src []T) []T

泛型函数

func FromStrings2

func FromStrings2[T any, PT Setter2[T]](s []string) []T

func Index

func Index[T comparable](s []T, x T) int

约束中的可比类型

func Print

func Print[T any](t T)

Go 泛型泛型语法为中括号

func Stringify

func Stringify[T Stringer](s []T) (ret []string)

func Sub

func Sub[T Types](t1, t2 T) T

限定为 Types

func Zero

func Zero[T any]() T

在该代码中,T可以是任何值,包括可能不为nil的值。 我们可以利用var语句来解决这个问题,它生成一个新变量,并将其初始化为该类型的零值:

Types

type Address

type Address string

type AnyString

type AnyString interface {
	~string
}

AnyString 就可以包括 Phone,Email,Address等类型 即: 基础类型为string的类型都可以

type ComparableHasher

type ComparableHasher interface {
	comparable
	Hash() uintptr
}

type Email

type Email string

type Integer

type Integer interface {
	int
}

其中 int 为基础类型

type Phone

type Phone string

近似约束元素 类型别名

type Queue

type Queue[T any] struct {
	// contains filtered or unexported fields
}

泛型零值 一般使用 var zero T 有一个对象,包含一个管道属性,可以调用此对象方法压入或弹出数据

func NewQueue

func NewQueue[T any](size int) Queue[T]

构建新的队列

func (Queue[T]) Pop

func (q Queue[T]) Pop() T

弹出数据 ,如果没有数据会被阻塞

func (Queue[T]) Push

func (q Queue[T]) Push(val T)

压入数据

func (Queue[T]) TryPop

func (q Queue[T]) TryPop() (T, bool)

根据这一特性,可以改写TryPop方法

type Settable

type Settable int

定义一个可设置的 int

func (*Settable) Set

func (p *Settable) Set(s string)

从字符串中设置 *p 的值

type Setter2

type Setter2[B any] interface {
	Set(string)
	*B // non-interface type constraint element
}

type SignedInteger

type SignedInteger interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

联合约束元素

func SumOfSignedInteger

func SumOfSignedInteger[T SignedInteger](integers []SignedInteger) SignedInteger

type Stringer

type Stringer interface {
	String() string
}

方法限制,接口约束

type Types

type Types interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 |
		~uint16 | ~uint32 | ~uint64 | ~uintptr | ~float32 | ~float64
}

类型集合

Jump to

Keyboard shortcuts

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