PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, December 16, 2022

[FIXED] What is the problem of Go in this context syntactically?

 December 16, 2022     go, syntax     No comments   

Issue

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

Solution

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.



Answered By - Harits Elfahmi
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing