-1

I was trying to write a function but the issue here surprised me.

userGroup.Use(
        middleware.BasicAuth(func(username, password string, c echo.Context) (bool, error)  {
            if username == "joe" && password == "123"{
                return true, nil
            }
            return false, nil
        })  // <- error happens here
    )
userGroup.Use(
        middleware.BasicAuth(func(username, password string, c echo.Context) (bool, error)  {
            if username == "joe" && password == "123"{
                return true, nil
            }
            return false, nil
        })) // <- !!

Spent an hour for a bug but turns out the last closing parentheses must not float around. Is this an issue with semicolons, commas or indentation? I remember JS not caring about this kind of stuff The error I was getting was :

missing ',' before newline in argument list |syntax
  • This error message may be more direct than you think it is. There is a missing ',' (comma). It should go before the newline (meaning at the end of the line) in the argument list (right where your arrow is pointing). – Hymns For Disco Jun 04 '22 at 05:49
  • See [How to break a long line of code in Golang?](https://stackoverflow.com/questions/34846848/how-to-break-a-long-line-of-code-in-golang/34848928#34848928) – icza Jun 04 '22 at 07:44
  • @HymnsForDisco but adding a comma won't change anything. It still gives an error. – thenewboston Jun 04 '22 at 14:29

1 Answers1

2

This is the result of Golang's semi-colon rule: https://go.dev/doc/effective_go#semicolons, where Go is adding a semicolon as it scans the source, so the original source is free of semicolon.

Following the rule "if the newline comes after a token that could end a statement, insert a semicolon", your earlier code would look like below:

userGroup.Use(
        middleware.BasicAuth(func(username, password string, c echo.Context) (bool, error)  {
            if username == "joe" && password == "123"{
                return true, nil
            }
            return false, nil
        });  // <- semicolon added here
    )

which of course wrong and causes an error. Moving the closing parentheses on that line instead fixes that.