Can golang variable parameters be used in generic functions?

PHPz
Release: 2024-04-29 14:06:01
Original
382 people have browsed it

In Go, variable parameters can be used for generic functions, allowing the creation of generic functions that accept a variable number of parameters and are suitable for multiple types. For example, you can create a generic function Mode that finds the most frequently occurring element in a given list: Mode accepts a variable number of elements of type T. It counts elements by creating counts for each element. Then it finds the element that appears the most and returns it as mode. In the main function, you can call the Mode function for a list of strings and a list of integers, which will return the most frequent string and number respectively.

Can golang variable parameters be used in generic functions?

Generic functions with variable parameters in Go

Preface

Generics Introduced in Go 1.18, it allows us to create functions and types that do not rely on parameters of a specific type. This blog post will explore whether variable parameters in Go are suitable for generic functions and provide a practical case.

Varameters

Varameter function is a function that accepts a variable number of parameters. In Go, variable parameters are usually expressed using the ... syntax, for example:

func Sum(nums ...int) int {
  sum := 0
  for _, num := range nums {
    sum += num
  }
  return sum
}
Copy after login

Generic function

Generic function is the parameter type Assignable functions. In Go, generic functions are expressed using the [] syntax, for example:

func Max[T comparable](values ...T) T {
  max := values[0]
  for _, value := range values {
    if value > max {
      max = value
    }
  }
  return max
}
Copy after login

Variable parameters and generics

Variable parameters and generics Can be used together in Go, allowing us to create generic functions that accept a variable number of arguments and work with multiple types.

Practical Case

Task Objective: Create a generic function to find the element that appears the most in a given list.

Code:

import "fmt"

func Mode[T comparable](values ...T) T {
  counts := map[T]int{}
  var mode T
  var maxCount int

  // 统计元素计数
  for _, value := range values {
    counts[value]++
  }

  // 找出出现次数最多的元素
  for value, count := range counts {
    if count > maxCount {
      maxCount = count
      mode = value
    }
  }

  return mode
}

func main() {
  // 字符串列表
  strs := []string{"apple", "orange", "apple", "pear", "banana"}
  fmt.Println("最常见的字符串:", Mode(strs...)) // apple

  // 整数列表
  nums := []int{1, 2, 3, 2, 4, 2, 5}
  fmt.Println("最常见的数字:", Mode(nums...)) // 2
}
Copy after login

Description:

  • Mode The function is a generic Function that accepts a variable number of elements of type T.
  • It returns the element with the most occurrences and counts the number of occurrences of each element in counts.
  • In the main function, we create two lists: a list of strings and a list of integers, and call the Mode function for each list.
  • The output shows that the most frequently occurring elements are "apple" (for the list of strings) and 2 (for the list of integers).

The above is the detailed content of Can golang variable parameters be used in generic functions?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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