In-depth understanding of generics in golang (Generic)
This article brings you an in-depth understanding of generics in golang? How to use generics? It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
What is generics
Generics (Generic) is a programming technology. In a strongly typed language, you are allowed to write code using types that are specified later, and specify the corresponding type at instantiation time.
In generics, type parameters can be used instead of specific data types. These type parameters can be declared in a class, method, or interface, and can be used in these declarations. Code that uses generics can specify the actual type parameters at runtime, which allows the code to be applied to many different types of data.
Generics can improve the readability, maintainability and reusability of code. It reduces the redundancy of your code and provides better type safety and compile-time type checking.
Let’s use a specific example to explain why generics can reduce code redundancy:
Provide a function that returns the minimum value of a and b. We need each Write a function for a specific data type "int, float..."; or use interface{} "It requires type assertion for parameters, which has an impact on running performance, and cannot constrain the parameters passed in"
func minInt(a, b int) int { if a > b { return b } return a } func minFloat(a, b float64) float64 { if a > b { return b } return a } func minItf(a, b interface{}) interface{} { switch a.(type) { case int: switch b.(type) { case int: if a.(int) > b.(int) { return b } return a } case float64: switch b.(type) { case float64: if a.(float64) > b.(float64) { return b } return a } } return nil }
We can see from the above method that except for the different types of parameters and return results, minInt and minFloat have the same code. Is there a way to determine the type passed in when the function is called without specifying a specific type? Here, a concept called generics is introduced, which can be simply understood as a broad type or an unspecified specific type. By introducing generics, we no longer need to specify specific data types. The min function can be used in the following way:
// T 为类型参数, 在调用时确定参数的具体值, 可以为 int, 也可以为 float64;它与 a, b 一样也是参数, 需要调用时传入具体的值;不同的是,T 为类型参数,值为具体的类型, a,b 为函数参数,值为具体类型对应的值 func minIntAndFloat64[T int | float64](a, b T) T { if a < b { return a } return b } minIntAndFloat64[int](1, 2) // 实例化/调用时指定具体的类型
Generics in go
go only introduced generics in version 1.8. If your go version is lower than 1.8, you cannot use generics. The code in this article uses version 1.9. In version 1.8, a lot of changes were made to support generics.
- Type parameters are introduced in function and type declarations
- A collection of types can be defined through interfaces, including types without methods
- Type derivation, in some scenarios Type parameters will be deduced, and you can call the function without specifying the value of the type parameter
Formal parameters, actual parameters, type parameters, type actual parameters, instantiation
Let’s look at a common add
function first. add
is the function name, x, y
is the formal parameter, (x, y int)
is the parameter list. When a function call occurs, add(2, 3)
2, 3 are actual parameters.
By analogy to generics, we need a type parameter. When a function call occurs, the corresponding type parameter is passed in. A function with type parameters is called a generic function.
[T int | int64]
is the type parameter list, T
is the type parameter, int | int64
is the type set/type constraint. When a function call occurs add[int](2,3)
, int is the type actual parameter. This call is also called instantiation, that is, the type actual parameter is determined.
Type parameters can also be specified when declaring the structure. MyStruct[T]
is a generic structure, and methods can be defined for generic structures.
Type collection, interface
In the basic type, uint8 represents a collection of 0~255. Then for type parameters, you also need to define a collection of types just like the basic type. In the above example int | string
is a collection of types. So how to reuse a collection of types? Interfaces are used here for definition. The following is the definition of a type collection. Therefore, we can define a generic function add[T Signed](x, y T) T
Before go 1.8, the definition of the interface It is a collection of methods, that is, if the method corresponding to the interface is implemented, it can be converted into the corresponding interface. In the following example, the MyInt
type implements the Add method and can therefore be converted to MyInterface
.
type MyInterface interface { Add(x, y int) int } type MyInt int func (mi myInt) Add(x, y int) int { return x + y } func main() { var mi MyInterface = myInt(1) fmt.Println(mi.Add(1, 2)) }
If we think about it from another angle, MyInterface
can be regarded as a type collection, which includes all types that implement the add
method. Then, MyInterface can be used as a collection of types. For example, we can define a generic function as follows.
func I[T MyInterface](x, y int, i T) int { return i.Add(x, y) }
在泛型中, 我们的类型集合不仅仅是实现接口中定义方法的类型, 还需要包含基础的类型。因此, 我们可以对接口的定义进行延伸, 使其支持基础类型。为了保证向前兼容, 我们需要对接口类型进行分类:
基础接口类型
只包含方法的集合, 既可以当作类型集合, 又可以作为数据类型进行声明。如下面的 MyInterface
。还有一个特殊的接口类型 interface{}, 它可以用来表示任意类型, 即所有的类型都实现了它的空方法。在 1.8 之后可以使用 any 进行声明。
type any = interface{} type MyInterface interface { Add(x, y int) int String() string String() string // 非法: String 不能重复声明 _(x int) // 非法: 必须要有一个非空的名字 }
接口组合
可以通过接口组合的形式声明新的接口, 从而尽可能的复用接口。从下面的例子可以看出, ReadWriter
是 Reader
和 Write
的类型集合的交集。
type Reader interface { Read(p []byte) (n int, err error) Close() error } type Writer interface { Write(p []byte) (n int, err error) Close() error } // ReadWriter's methods are Read, Write, and Close. type ReadWriter interface { Reader // includes methods of Reader in ReadWriter's method set Writer // includes methods of Writer in ReadWriter's method set }
通用接口
上面说的接口都必须要实现具体的方法, 但是类型集合中无法包含基础的数据类型。如: int, float, string...。通过下面的定义, 可以用来表示包含基础数据类型的类型集合。在 golang.org/x/exp/constraints
中定义了基础数据类型的集合。我们可以看到 ~
符号, 它表示包含潜在类型为 int | int8 | int16 | int32 | int64 的类型, |
表示取并集。Singed
就表示所有类型为 int 的类型集合。
// Signed is a constraint that permits any signed integer type. // If future releases of Go add new predeclared signed integer types, // this constraint will be modified to include them. type Signed interface { ~int | ~int8 | ~int16 | ~int32 | ~int64 } type myInt int // 潜在类型为 int func add[T constraints.Integer](x, y T) T { return x + y } func main() { var x, y myInt = 1, 2 fmt.Println(add[myInt](x, y)) }
下面来看一些特殊的定义
// 潜在类型为 int, 并且实现了 String 方法的类型 type E interface { ~int String() string } type mInt int // 属于 E 的类型集合 func (m mInt) String() string { return fmt.Sprintf("%v", m) } // 潜在类型必须是自己真实的类型 type F interface { ~int // ~mInt invalid use of ~ (underlying type of mInt is int) // ~error illegal: error is an interface } // 基础接口可以作为形参和类型参数类型, 通用类型只能作为类型参数类型, E 只能出现在类型参数中 [T E] var x E // illegal: cannot use type E outside a type constraint: interface contains type constraints var x interface{} = E(nil) // illegal: cannot use interface E in conversion (contains specific type constraints or is comparable)
类型推导
由于泛型使用了类型参数, 因此在实例化泛型时我们需要指定类型实参。 看下面的 case, 我们在调用函数的时候并没有指定类型实参, 这里是编译器进行了类型推导, 推导出类型实参, 不需要显性的传入。
func add[T constraints.Integer](x, y T) T { return x + y } func main() { fmt.Println(add(1, 1)) // add[int](1,1) }
有时候, 编译器无法推导出具体类型。则需要指定类型, 或者更换写法, 也许可以推断出具体类型。
// 将切片中的值扩大 func Scale[E constraints.Integer](s []E, c E) []E { r := make([]E, len(s)) for i, v := range s { r[i] = v * c } return r } func ScaleAndPrint(p Point) { r := Scale(p, 2) r.string() // 非法, Scale 返回的是 []int32 } type Point []int32 func (p Point) string() { fmt.Println(p) } // 方法更新,这样传入的是 Point 返回的也是 Point func Scale[T ~[]E, E constraints.Integer](s T, c E) T { r := make([]E, len(s)) for i, v := range s { r[i] = v * c } return r }
泛型的使用
go 是在 1.8 版本中开始引入泛型的。下面主要介绍一下什么时候使用泛型:
内置容器类型
在 go 中, 提供以下容器类型:map, slice, channel。当我们用到容器类型时, 且逻辑与容器具体的类型无关, 这个时候可以考虑泛型。这样我们可以在调用时指定具体的类型实参, 从而避免了类型断言。例如,下面的例子, 返回 map 中的 key。
// comparable 是一个内置类型, 只能用于对类型参数的约束。在 map 中, key 必须是可比较类型。 func GetKeys[K comparable, V any](m map[K]V) []K { res := make([]K, 0, len(m)) for k := range m { res = append(res, k) } return res }
通用的结构体
对于一些通用的结构体, 我们应该使用泛型。例如, 栈、队列、树结构。这些都是比较通用的结构体, 且逻辑都与具体的类型无关, 因此需要使用泛型。下面是一个栈的例子:
type Stack[T any] []T func (s *Stack[T]) Push(item T) { *s = append(*s, item) } func (s *Stack[T]) Pop() T { if len(*s) == 0 { panic("can not pop item in emply stack") } lastIndex := len(*s) - 1 item := (*s)[lastIndex] *s = (*s)[:lastIndex] return item } func main() { var s Stack[int] s.Push(9) fmt.Println(s.Pop()) s.Push(9) s.Push(8) fmt.Println(s.Pop(), s.Pop()) }
通用的函数
有些类型会实现相同的方法, 但是对于这些类型的处理逻辑又与具体类型的实现无关。例如: 两个数比大小, 只要实现 Ordered 接口即可进行大小比较:
func Min[T constraints.Ordered](x, y T) T { if x < y { return x } return y } func main() { fmt.Println(Min(5, 6)) fmt.Println(Min(6.6, 9.9)) }
总结
go 在引入泛型算是一次较大的改动。我们只有弄清楚类型参数、类型约束、类型集合、基础接口、通用接口、泛型函数、泛型类型、泛型接口等概念, 才能不会困惑。核心改动点还是引入了类型参数, 使用接口来定义类型集合。
当然,也不能为了使用泛型而使用泛型。还是要具体的 case 具体来分析。 简单的指导原则就是, 当你发现你的代码除了类型不同外, 其余代码逻辑都相同; 或者你写了许多重复代码, 仅仅是为了支持不同类型; 那么你可以考虑使用泛型。
推荐学习:Golang教程
The above is the detailed content of In-depth understanding of generics in golang (Generic). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In Go, WebSocket messages can be sent using the gorilla/websocket package. Specific steps: Establish a WebSocket connection. Send a text message: Call WriteMessage(websocket.TextMessage,[]byte("Message")). Send a binary message: call WriteMessage(websocket.BinaryMessage,[]byte{1,2,3}).

Memory leaks can cause Go program memory to continuously increase by: closing resources that are no longer in use, such as files, network connections, and database connections. Use weak references to prevent memory leaks and target objects for garbage collection when they are no longer strongly referenced. Using go coroutine, the coroutine stack memory will be automatically released when exiting to avoid memory leaks.

In Go, the function life cycle includes definition, loading, linking, initialization, calling and returning; variable scope is divided into function level and block level. Variables within a function are visible internally, while variables within a block are only visible within the block.

In Go, you can use regular expressions to match timestamps: compile a regular expression string, such as the one used to match ISO8601 timestamps: ^\d{4}-\d{2}-\d{2}T \d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-][0-9]{2}:[0-9]{2})$ . Use the regexp.MatchString function to check if a string matches a regular expression.

Go and the Go language are different entities with different characteristics. Go (also known as Golang) is known for its concurrency, fast compilation speed, memory management, and cross-platform advantages. Disadvantages of the Go language include a less rich ecosystem than other languages, a stricter syntax, and a lack of dynamic typing.

View Go function documentation using the IDE: Hover the cursor over the function name. Press the hotkey (GoLand: Ctrl+Q; VSCode: After installing GoExtensionPack, F1 and select "Go:ShowDocumentation").

Unit testing concurrent functions is critical as this helps ensure their correct behavior in a concurrent environment. Fundamental principles such as mutual exclusion, synchronization, and isolation must be considered when testing concurrent functions. Concurrent functions can be unit tested by simulating, testing race conditions, and verifying results.

Writing clear and comprehensive documentation is crucial for the Golang framework. Best practices include following an established documentation style, such as Google's Go Coding Style Guide. Use a clear organizational structure, including headings, subheadings, and lists, and provide navigation. Provides comprehensive and accurate information, including getting started guides, API references, and concepts. Use code examples to illustrate concepts and usage. Keep documentation updated, track changes and document new features. Provide support and community resources such as GitHub issues and forums. Create practical examples, such as API documentation.
