单值上下文中的多值ERROR [英] Multiple-value in single-value context ERROR

查看:58
本文介绍了单值上下文中的多值ERROR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在编译我的GO代码时遇到此错误:

I got this error while compiling my GO code:

multiple-value fmt.Println() in single-value context

我正在尝试创建一个函数,该函数接受可变数量的int并在一行上打印每个变量.

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
}

嗯,怎么了?

推荐答案

查看 http://godoc.org/fmt#Println

fmt.Println 返回多个值.int和and错误:

fmt.Println returns multiple values.. an int and and error:

func Println(a ... interface {})(n int,错误,错误)

您仅分配给int.试试这个:

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
}

这篇关于单值上下文中的多值ERROR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆