Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8278365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:59:08+00:00 2026-06-08T08:59:08+00:00

Does a go block following a for, func or if statement have to have

  • 0

Does a go block following a for, func or if statement have to have the opening brace on the same line? I get a compile error if I move it down but I can’t see in the language spec where they show that a block has to be structured like that.

A block is a sequence of declarations and statements within matching
brace brackets.

Block = “{” { Statement “;” } “}” .

IfStmt = “if” [ SimpleStmt “;” ] Expression Block [ “else” ( IfStmt |
Block ) ] .

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T08:59:10+00:00Added an answer on June 8, 2026 at 8:59 am

    From Effective Go, because of semicolon inference:

    One caveat.
    You should never put the opening brace of a control structure (if, for, switch, or select) on the next line.
    If you do, a semicolon will be inserted before the brace, which could cause unwanted effects. Write them like this:

    if i < f() {
        g()
    }
    

    not like this:

    if i < f()  // wrong!
    {           // wrong!
        g()
    }
    

    As jnml comments, the language syntax is correct for blocks.
    But combined with Semicolon injection, it means you should really:

    • always put the brace on the same line than the if statement (or the ‘if‘ won’t do what you think it should)
    • actually, always use gofmt and don’t think about it (Preferably, gofmt your code each time you save it in your editor. It is fast and will make your code consistent with the rest of any Go code out there)

    Even the Go compiler will enforce that “same line for brace” rule, to avoid any unforeseen side-effect.
    So the language reference doesn’t say where to put the brace, but both gofmt and the compiler will make sure it is correctly placed for a if statement.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following block of code ///////////////////////////////////// CComVariant newVal; //pass the CComVariant and get
I currently have the following code: events.detect do |event| #detect does the block until
What does {{ ... }} block mean in the following code? class X {
I have the following block of code in a makefile: param_test_dir: @if test -d
Can someone explain why the following line of C# doesn't behave the same as
I'm running following block in Oracle (what does it do is not so important)
I have the following block of code that retrieves a document node in kentico
Possible Duplicate: Does a finally block always run? let's imagine the following scenario: public
Short and sweet: Why does the following block of Objective-C code not produce a
In the following block of code, does VB.NET gracefully exit the With block if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.