ProgramGuru

Syntax

Start with Go Syntax

Syntax is the set of rules for writing valid Go code. Go uses braces to mark blocks, parentheses for function calls and newlines to make statements easy to read.

What you’ll learn

  • Identify statements and blocks.
  • Distinguish declaration from assignment.
  • Follow Go’s brace and formatting conventions.

Understand the idea

A statement such as score := 90 defines a variable inside a function. Function bodies are wrapped in { }. Go generally inserts statement-ending semicolons automatically, so you rarely write them.

The opening brace for a function or an if must stay on the same line as its declaration or condition. Go formatting is standardized, and gofmt can apply it automatically.

Visualize how it works

Follow this diagram before executing the example. Check where a value or decision changes what runs next.

Visual explanation for syntaxA simple source file has a predictable structure.Package declarationStep 1Import packagesStep 2Declare mainStep 3Execute statementsStep 4A simple source file has a predictable structure.
A simple source file has a predictable structure.

Run a complete Go example

This example is a complete program. Save it as main.go or paste it into the Go Playground.

Go code

package main

import "fmt"

func main() {
	score := 90
	fmt.Println("Score:", score)
	score = 95
	fmt.Println("Updated:", score)
}

Output

Score: 90
Updated: 95

Trace what the program does

The short declaration creates score. After the first print, score = 95 assigns a new value to the existing variable. The two calls execute in order; there is no need to type semicolons at the ends of these lines.

Avoid common mistakes

  • Do not write func main() on one line and put its opening brace on the next.
  • A compiler error about an unused local variable or import is intentional: Go rejects unused declarations in these contexts.

Try it yourself

Change the variable name to points consistently, and use gofmt to check the formatting.

Tip: Edit the example above, predict the output, and run the modified code to test your reasoning.

Check your understanding

Why are semicolons usually absent from ordinary Go source?
Choose one answer. You can try again.

Answer and explanation

The lexer normally inserts them at statement boundaries

Correct. The short declaration creates score.

What you should remember

You have practiced Go Syntax in a small, self-contained program. The important habit is to predict what runs, inspect the actual output and distinguish the language rule from the particular example.

Before continuing, make sure you can explain the program to someone who has not seen it, including the line that changes its result.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Comments →