Golang generic + variable parameter function

WBOY
Release: 2024-02-05 21:21:15
forward
920 people have browsed it

Golang 泛型+可变参数函数

Question content

In golang, there are some general types of functions

type transformer[a, b any] func(a)(b, error)
Copy after login

How to define a general variable parameter higher-order function, which can usually be composed of a function like this

func Compose[A,B,C....N any](transformers... Transformer[A,B], Transformer[B,C]...Transformer[M,N]) Transformer[A,N]
Copy after login

Correct answer


In Go, universal variadic functions are not yet supported. However, you can achieve similar results by using variadic arguments and recursion.

The above is the detailed content of Golang generic + variable parameter function. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template