Golang a generic function that finds the smallest number in an array

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.