Make it a generic function!

Report a typo

Below you will see a variadic function multiply() that can take many int type arguments, multiply them all together, and finally return the calculated total:

func multiply(nums ...int) int {
    total := 1
    for _, num := range nums {
        total *= num
    }
    return total
}

You will see a generic function template for multiply() below. Your task is to write the additional code required to make multiply() a generic function that can only take int and float64 data types. Remember what you've learned about combined type constraints!

Sample Input 1:

5 25 625
1.5 2.5 3.5

Sample Output 1:

78125
13.125
Write a program in Go
package main

import "fmt"

// multiply is a variadic function
// make multiply a generic function that can take int and float64 data types only!
func multiply[T ? ? ?](nums ...?) ? {
// Do not change the body of the multiply() function!
var total T = 1
for _, num := range nums {
total *= num
}
return total
}

// Do not change anything in the main function!
func main() {
var num1, num2, num3 int
fmt.Scanln(&num1, &num2, &num3)

var num4, num5, num6 float64
fmt.Scanln(&num4, &num5, &num6)

fmt.Println(multiply(num1, num2, num3))
fmt.Println(multiply(num4, num5, num6))
}
___

Create a free account to access the full topic