How to solve “too many arguments to return” issue in Golang?

后端 未结 2 1712
你的背包
你的背包 2020-12-17 08:01

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.

相关标签:
2条回答
  • 2020-12-17 08:38

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

    0 讨论(0)
  • 2020-12-17 08:44

    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

    0 讨论(0)
提交回复
热议问题