Golang to implement a generic function to find the minimum number of arrays, you need to use a type switch.
GOL Type switch notation:
Switch V. (type) {case Int:case float64:case string:}
The idea is to iterate over the array, to save the smaller number, and the function exits to return that number.
Golang Code:
package mainimport ( "FMT") Func minimum (first interface{}, rest ...interface{}) interface{}{ minimum := first for _, v := range rest { switch v. (type) { case int: if v := v. (int); v < minimum. (int) { minimum = v } case float64: &nbSp; if v := v. (float64); v < minimum. (float64) { minimum = v } case string: if v := v. (string); v < minimum. (string) { minimum = v } } } return minimum}func main () { i := minimum (1, 3, 5, 7, 9, 10, -1, 1). (int) fmt. Printf ("i = %d\n", i)}
Clang
#include <stdio.h>int min_int (VOID&NBSP;*V1,&NBSP;VOID&NBSP;*V2) { return * (int *) v1 > * (int *) v2;} Int min_double (VOID&NBSP;*V1,&NBSP;VOID&NBSP;*V2) { return * (double *) v1 > * (double *) v2;} Void *minimum (void *base, int size, int esize, int (*min_cmp) (void *, void *)) { int i; void *mini = base; for (i = 1; i < size; i++) { if (MIN_CMP (mini, base + i * esize)) mini = base + i * esize; } return mini;} #define arr_len (ARR) &Nbsp;sizeof (arr) / sizeof (arr) [0]) #define &NBSP;ARR_ESIZE (arr) sizeof (arr) [0]) Int main () { void *min; int arr_int[] = {0, 5, 6, 7, 8, 9, -1, 2}; min = minimum (arr_ Int, arr_len (Arr_int), arr_esize (Arr_int), min_int); printf ("%d\n", * (int *) min); double arr_double[] = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, -3.14, 99}; min = minimum (arr_double , arr_len (arr_double), arr_esize (arr_double), min_double); printf ("%lf\n ", * (double *) min); return 0;}
Golang a generic function that finds the smallest number in an array