Go — Syntax
Package declaration
Every Go file starts with a package declaration:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
Import statements
import "fmt"
import "os"
// Grouped imports
import (
"fmt"
"os"
"strings"
)
// Import with alias
import (
myfmt "fmt"
"os"
)
// Blank import (for side effects)
import _ "github.com/lib/pq"
Variables
// var declaration
var name string = "Alice"
var age int
// Short declaration (inside functions only)
name := "Alice"
age := 30
// Multiple declarations
var (
x int = 10
y int = 20
)
// Multiple short declarations
x, y := 10, 20
// Blank identifier
_ = someFunction() // Ignore return value
Constants
const Pi = 3.14159
const MaxRetries = 3
// Typed constants
const (
StatusOK int = 200
StatusError int = 500
)
// iota for enumeration
const (
Sunday = iota // 0
Monday // 1
Tuesday // 2
Wednesday // 3
Thursday // 4
Friday // 5
Saturday // 6
)
// iota with expressions
const (
_ = iota
KB = 1 << (10 * iota) // 1024
MB // 1048576
GB // 1073741824
)
Functions
// Basic function
func add(a int, b int) int {
return a + b
}
// Multiple return values
func divide(a, b float64) (float64, error) {
if b == 0 {
return 0, fmt.Errorf("division by zero")
}
return a / b, nil
}
// Named return values
func swap(a, b int) (int, int) {
return b, a
}
// Variadic function
func sum(nums ...int) int {
total := 0
for _, n := range nums {
total += n
}
return total
}
Control flow
// if/else
if age >= 18 {
fmt.Println("Adult")
} else {
fmt.Println("Minor")
}
// if with init statement
if err := doSomething(); err != nil {
fmt.Println("Error:", err)
}
// for loop (only loop construct)
for i := 0; i < 5; i++ {
fmt.Println(i)
}
// while-style
for count < 10 {
count++
}
// infinite loop
for {
break
}
// range
for i, v := range []string{"a", "b", "c"} {
fmt.Println(i, v)
}
// switch
switch day {
case "Monday":
fmt.Println("Start of week")
case "Friday":
fmt.Println("Almost weekend")
default:
fmt.Println("Midweek")
}
// type switch
switch v := i.(type) {
case int:
fmt.Println("int:", v)
case string:
fmt.Println("string:", v)
}
Structs
type Person struct {
Name string
Age int
}
// Creation
p1 := Person{Name: "Alice", Age: 30}
p2 := new(Person) // pointer
Methods
func (p Person) Greet() string {
return "Hello, " + p.Name
}
func (p *Person) SetAge(age int) {
p.Age = age
}
Error handling
result, err := doSomething()
if err != nil {
fmt.Println("Error:", err)
return
}
Mini Practice
Write Go code that:
- Declares variables with
varand:= - Creates a function with multiple return values
- Uses a
forloop withrange - Handles an error with
if err != nil
Up Next
In the next lesson, you'll learn about Comments — documenting your Go code.
Related Topics
Frequently Asked Questions about Syntax
What is Syntax in Go?
Syntax is a fundamental concept in Go. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Syntax?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Syntax.
Why is Syntax important in Go?
Syntax is essential for Go development. Understanding this concept will help you write better code and solve real-world problems more effectively.