I got this error while compiling my GO code:
multiple-value fmt.Println() in single-value context
I'm trying to create a function that takes in variable number of ints and prints each variable on a line.
GO:
package main
import (
"fmt"
)
func main() {
slice := []int{1,3,4,5}
vf(slice...)
}
func vf(a ...int) int {
if len(a)==0 {
return 0
}
var x int
for _, v := range a {
x = fmt.Println(v)
}
return x
}
Hmm what's wrong?
Check out http://godoc.org/fmt#Println
fmt.Println
returns multiple values.. an int and and error:
func Println(a ...interface{}) (n int, err error)
You are only assigning to the int. try this:
package main
import (
"fmt"
)
func main() {
slice := []int{1,3,4,5}
vf(slice...)
}
func vf(a ...int) int {
if len(a)==0 {
return 0
}
var x int
for _, v := range a {
x, _ = fmt.Println(v)
}
return x
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With