generic

package
v0.0.0-...-4cb5900 Latest Latest
Warning

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

Go to latest
Published: May 17, 2023 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenericPrint

func GenericPrint[T any](nums []T)

通用类型

func GenericSum

func GenericSum[T Number](nums []T) T

范型函数,范例1

func GenericSum1

func GenericSum1[T int64 | float64](nums []T) T

范型函数,范例2 【这种方式如果类型很多,代码可读性不好,所以定义一个Number接口代替】

func PrintInfo

func PrintInfo[T Info](info T)

定义打印Name的范型函数:最简单的理解方式: Type T = Info,其实T就是Info的别名

Types

type FileWriter

type FileWriter[T any] struct {
}

func (FileWriter[T]) Write

func (t FileWriter[T]) Write(data T)

type Info

type Info interface {
	Name() string
}

type Number

type Number interface {
	int64 | float64
}
范型demo https://blog.csdn.net/neweastsun/article/details/128275384

Number 必须为接口

type ProductInfo

type ProductInfo struct {
}

func (ProductInfo) Name

func (t ProductInfo) Name() string

type UserInfo

type UserInfo struct {
}

func (UserInfo) Name

func (t UserInfo) Name() string

type Writer

type Writer[T any] interface {
	Write(data T)
}

Jump to

Keyboard shortcuts

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