在我正在编写的打印函数中,我试图基于switch语句的结果返回一个值;但是,我得到的错误太多,无法返回。

如果这个问题的答案很简单,请原谅我,但函数有多少个参数可以返回一件事,这不应该吗?还是需要为每个参数返回一件事。

这是我的代码。我在返回行上收到错误(返回的参数过多)。如何修复它,使其返回在switch语句中设置的字符串?

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
}

最佳答案

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

这:

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

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

推荐读物:
  • http://golang.org/doc/effective_go.html#multiple-returns
  • http://golang.org/doc/effective_go.html#named-results

  • 甚至所有effective go

    关于go - 如何解决Golang中的 “too many arguments to return”问题?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25455233/

    10-12 07:40
    查看更多