如何解决“太多争论返回”问题在Golang? [英] How to solve "too many arguments to return" issue in Golang?

查看:108
本文介绍了如何解决“太多争论返回”问题在Golang?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我写的打印函数中,我试图根据switch语句的结果返回一个值;然而,我得到的错误太多的参数返回。



原谅我,如果这个问题有一个简单的答案,但不应该有多少参数一个函数有,它只能返回一件事?或者它需要为每个参数返回一件事情。



这是我的代码。我在返回行出现错误(返回的参数过多)。如何修复它,以便它返回在switch语句中设置的字符串?

 包托架

func打印(DATA [] TD,包含字符串,排除[]字符串,字符串){
结果:= NBC(DATA,包含,排除,str)
var发送字符串
开关结果{
案例1:
sentAnal =强烈否定
案例2:
sentAnal =非常消极
案例3:
sentAnal =负数
案例4:
sentAnal =小负数
案例5:
sentAnal =Neurtral
案例6:
sentAnal =小正面
案例7:
sentAnal =正面
案例8:
sentAnal =更积极
案例9:
sentAnal =非常正面
案例10:
sentAnal =强烈正面
默认值:
sentAnal =未知
}
返回sentAnal
}


解决方案

您需要在指定输入参数后指定要返回的内容,这不是python。





  func打印(DATA [] TD,包括字符串,排除[]字符串,字符串){
code>

应该是:

  func Print(DATA [] TD,包括字符串,排除[]字符串,字符串)字符串{

推荐阅读:


  • http://golang.org/doc/effective_go.html#multiple-returns

  • http://golang.org/doc/effective_go.html#named-results


  • ul>

    甚至所有的有效去


    In a print function I am writing, I am trying to return a value based on the result of a switch statement; however, I am getting the error too many arguments to return.

    Forgive me if this question has a simple answer, but shouldn't it not matter how many arguments a function has and it can return just one thing? Or does it need to return one thing for each argument.

    Here is my code. I am getting an error on the return line ( Too many arguments to return ). How can I fix it so that it returns the string set in the switch statement?

    package bay
    
    func Print(DATA []TD, include string, exclude []string, str string) {
        result := NBC(DATA, include, exclude, str)
        var sentAnal string
        switch result {
        case 1:
            sentAnal = "Strongly Negative"
        case 2:
            sentAnal = "Very Negative"
        case 3:
            sentAnal = "Negative"
        case 4:
            sentAnal = "Little Negative"
        case 5:
            sentAnal = "Neurtral"
        case 6:
            sentAnal = "Little Positive"
        case 7:
            sentAnal = "Positive"
        case 8:
            sentAnal = "More Positive"
        case 9:
            sentAnal = "Very Positive"
        case 10:
            sentAnal = "Strongly Positive"
        default:
            sentAnal = "Unknown"
        }
        return sentAnal
    }
    

    解决方案

    You need to specify what you will return after specifying the input parameters, this is not python.

    This:

    func Print(DATA []TD, include string, exclude []string, str string) {
    

    Should be:

    func Print(DATA []TD, include string, exclude []string, str string) string {
    

    Recommended reads:

    Or even all of effective go

    这篇关于如何解决“太多争论返回”问题在Golang?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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