wordSmith
wordSmith

Reputation: 3183

How to solve "too many arguments to return" issue

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
}

Upvotes: 46

Views: 56764

Answers (3)

Bal Krishna Jha
Bal Krishna Jha

Reputation: 7286

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.

Upvotes: 3

fabmilo
fabmilo

Reputation: 48330

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

Upvotes: 4

Marc
Marc

Reputation: 1820

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

Upvotes: 77

Related Questions