英文:
How to solve "too many arguments to return" issue
问题
在我写的一个打印函数中,我试图根据switch语句的结果返回一个值;然而,我得到了错误信息"返回的参数太多"。
如果这个问题有一个简单的答案,请原谅我,但是函数有多少个参数并不重要,它只能返回一个东西,对吗?还是说它需要为每个参数返回一个东西。
这是我的代码。我在return语句上得到了一个错误(返回的参数太多)。我该如何修复它,以便它返回在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 = "强烈负面"
case 2:
sentAnal = "非常负面"
case 3:
sentAnal = "负面"
case 4:
sentAnal = "稍微负面"
case 5:
sentAnal = "中性"
case 6:
sentAnal = "稍微积极"
case 7:
sentAnal = "积极"
case 8:
sentAnal = "更积极"
case 9:
sentAnal = "非常积极"
case 10:
sentAnal = "非常积极"
default:
sentAnal = "未知"
}
return sentAnal
}
英文:
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
}
答案1
得分: 76
在指定输入参数后,你需要明确指定将返回的内容,这不是Python语法。
这段代码:
func Print(DATA []TD, include string, exclude []string, str string) {
应该改为:
func Print(DATA []TD, include string, exclude []string, str string) string {
推荐阅读:
-
effective go 的全部内容
-
effective go 中的 "Multiple Returns" 部分
-
effective go 中的 "Named Results" 部分
英文:
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
答案2
得分: 4
你指定的方法签名没有包含返回值。
func Print(DATA []TD, include string, exclude []string, str string) {
如果你想返回一个字符串,你需要添加返回值的类型。
func Print(DATA []TD, include string, exclude []string, str string) string {
请记住,在Go语言中,你可以返回多个值。
func Print(DATA []TD, include string, exclude []string, str string) (string, string) {
你甚至可以给返回值命名,并在代码中引用它。
func Print(DATA []TD, include string, exclude []string, str string) (sentAnal string) {
英文:
The signature of the method you specified does not include a return value
func Print(DATA []TD, include string, exclude []string, str string) {
if you want to return a string you need to add the type of the return value
func Print(DATA []TD, include string, exclude []string, str string) string {
Keep in mind in GO you can return multiple values
func Print(DATA []TD, include string, exclude []string, str string) (string, string) {
You can even give a name to the return value and reference it in your code
func Print(DATA []TD, include string, exclude []string, str string) (sentAnal string) {
答案3
得分: 3
如果你已经将返回类型指定为string,那么在返回语句中应该使用fmt.Sprintf,而不是fmt.Printf。
因为fmt.Printf的返回类型是(n int, err error),而fmt.Sprintf的返回类型是string。
这并没有回答OP的问题,但对其他人可能有帮助。
英文:
If you've mentioned return type as string then you should use fmt.Sprintf, not fmt.Printf in return statement.
Since retun type of fmt.Printf is (n int, err error) and return type of fmt.Sprintf is string.
It doesn't answer the OP question but maybe helpful to others.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论