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.
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: 95Trace 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
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.