Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve "too many arguments to return" issue

Tags:

go

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 } 
like image 359
wordSmith Avatar asked Aug 22 '14 20:08

wordSmith


People also ask

How do you fix too many arguments in a function?

Solution. So to fix the error “You've entered too many arguments for this function” you need to go through the content of the cell, character-by-character, to ensure that there are no syntax errors. In other words, you need to check whether all opened brackets are closed and all commas are properly in place.

What does it mean too many arguments in a function?

The too many arguments to function error occur because of these arguments in a function. The number of arguments should be the same in the function call and the function declaration of a program. We get this error when the number of arguments is different in the function declaration and function call.


2 Answers

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:

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

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

Or even all of effective go

like image 195
Marc Avatar answered Sep 18 '22 19:09

Marc


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) {

like image 32
fabrizioM Avatar answered Sep 17 '22 19:09

fabrizioM