2

I have an issue with my function. I'm getting a

final function parameter must have type

For this method

func (s *BallotaApi) PostUser(c endpoints.Context,userReq Users) (userRes Users, error) {

    c.Debugf("in the PostUser method")

    user := userManger.login(userReq)//return a Users Type 

    return user, nil

I read those threads but I can't figure out where I'm wrong. It looks like I declared everything.

can-you-declare-multiple-variables-at-once-in-go

go-function-declaration-syntax

1 Answer 1

10

If you are naming your return parameters, you must name all of them

(userRes Users, err error)

That way, return statements can apply.

As mentioned in Function type:

Within a list of parameters or results, the names (IdentifierList) must either all be present or all be absent.

If you try to name one and not the other, as in this example, you will get:

func a() (b int, error) {
    return 0, nil
}
# command-line-arguments
/tmp/sandbox170113103/main.go:9: final function parameter must have type

Dave C reminds us that:

Named returns should normally be limited to helping make better/clearer godoc documentation or when you need to change return values in a deferred closure.
Other than that they should be avoided.

Sign up to request clarification or add additional context in comments.

2 Comments

Named returns should normally be limited to helping make better/clearer godoc documentation or when you need to change return values in a deferred closure. Other than that they should be avoided.
@DaveC I agree. I have included your comment in the answer for more visibility.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.