Home >Backend Development >Golang >Golang generic + variable parameter function

Golang generic + variable parameter function

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBforward
2024-02-05 21:21:15987browse

Golang 泛型+可变参数函数

Question content

In golang, there are some general types of functions

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

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]

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!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete