Providing type argument

Report a typo

Emma has created a generic function subtract, which returns the total subtraction of zero to many Number type values:

type Number interface {
    int | float64 | complex128
}

func subtract[T Number](nums ...T) T {
    var total T
    for _, num := range nums {
        total -= num
    }
    return total
}

However, when Emma tried to call subtract within the main function of her program, she got the following error:

...

func main() {
    x := subtract(1.0, 2.0, 3)
    fmt.Println(x)
}

//Error: default type int of 3 does not match inferred type float64 for T

Write below the correct type argument syntax for float64 types Emma should provide to the subtract function, so the Go compiler doesn't throw any type inference errors.

Take notice that your answer must only include subtract?(1.0, 2.0, 3) and not the variable x := declaration.

Enter a short text
___

Create a free account to access the full topic