Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple-value in single-value context ERROR

Tags:

go

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?

like image 756
AUL Avatar asked Jul 29 '14 14:07

AUL


1 Answers

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
}
like image 88
aschepis Avatar answered Nov 13 '22 23:11

aschepis