douejuan9162 2019-01-31 07:13
浏览 84
已采纳

我收到关于没有足够参数返回的错误消息

I want return string or slice at one function, I can use in php. but, get some error in Go

package main

import "fmt"

func main() {
    // arr := []int {34, 23, 45, 56, 62, 45, 12, 96, 22}
    arr := []int{}

    fmt.Println(test(arr))
}

func test(dataList []int ) ( string ,  []int )  {
    if dataList == nil{
       return "this is string"
    }

    return []int{}
}

error: not enough arguments to return

  • 写回答

1条回答 默认 最新

  • ds753947 2019-01-31 07:55
    关注

    If you define two return types, you always need to return the two types. You can not return only one.

    Have a look at this:

    func test(dataList []int) (string, []int) {
        if dataList == nil {
            return "this is string", []int{}
        }
    
        return "", []int{}
    }
    

    This would work, because I am returning both a string and an []int in every case (and in exact the defined order).

    Maybe if you can explain what your function tries to accomplish, we can give better advice on how to design the function.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)