Go Syntax
Learn Go's clean and simple syntax rules
📝 Go Syntax Basics
Go syntax is designed to be clean, simple, and readable. With minimal punctuation and clear structure, Go makes programming intuitive for beginners while remaining powerful for experts.
// Go syntax is clean and simple
package main
import "fmt"
func main() {
message := "Go syntax is easy!"
fmt.Println(message)
}
Output:
Go syntax is easy!
Basic Syntax Rules
Package Declaration
Every Go file starts with package
package main
Import Statements
Import packages you need
import "fmt"
import "time"
Main Function
Program entry point
func main() {
// code here
}
Case Sensitive
Go is case sensitive
name != Name
fmt != Fmt
🔹 Variables and Types
Go has several ways to declare variables:
🔸 Variable Declaration
package main
import "fmt"
func main() {
// Method 1: var keyword with type
var name string = "Alice"
var age int = 25
// Method 2: var keyword without type (inferred)
var city = "New York"
var isActive = true
// Method 3: Short declaration (most common)
country := "USA"
score := 95.5
fmt.Println("Name:", name)
fmt.Println("Age:", age)
fmt.Println("City:", city)
fmt.Println("Active:", isActive)
fmt.Println("Country:", country)
fmt.Println("Score:", score)
}
Output:
Name: Alice
Age: 25
City: New York
Active: true
Country: USA
Score: 95.5
🔹 Basic Data Types
Go has several built-in data types:
Common Types:
- string: Text data
- int: Whole numbers
- float64: Decimal numbers
- bool: true or false
package main
import "fmt"
func main() {
// String
greeting := "Hello, Go!"
// Integer
count := 42
// Float
price := 19.99
// Boolean
isReady := true
// Print with types
fmt.Printf("String: %s (type: %T)\n", greeting, greeting)
fmt.Printf("Integer: %d (type: %T)\n", count, count)
fmt.Printf("Float: %.2f (type: %T)\n", price, price)
fmt.Printf("Boolean: %t (type: %T)\n", isReady, isReady)
}
Output:
String: Hello, Go! (type: string)
Integer: 42 (type: int)
Float: 19.99 (type: float64)
Boolean: true (type: bool)
🔹 Functions
Functions in Go are declared with the
func
keyword:
package main
import "fmt"
// Function with parameters and return value
func add(a int, b int) int {
return a + b
}
// Function with multiple return values
func divide(a, b float64) (float64, error) {
if b == 0 {
return 0, fmt.Errorf("cannot divide by zero")
}
return a / b, nil
}
// Function with named return values
func getInfo() (name string, age int) {
name = "Go Developer"
age = 30
return // automatically returns name and age
}
func main() {
// Call functions
sum := add(10, 20)
fmt.Println("Sum:", sum)
result, err := divide(10, 3)
if err != nil {
fmt.Println("Error:", err)
} else {
fmt.Printf("Division: %.2f\n", result)
}
name, age := getInfo()
fmt.Printf("Info: %s, %d years old\n", name, age)
}
Output:
Sum: 30
Division: 3.33
Info: Go Developer, 30 years old
🔹 Control Structures
Go has simple control flow statements:
🔸 If Statements
package main
import "fmt"
func main() {
age := 18
// Simple if
if age >= 18 {
fmt.Println("You are an adult")
}
// If-else
score := 85
if score >= 90 {
fmt.Println("Grade: A")
} else if score >= 80 {
fmt.Println("Grade: B")
} else {
fmt.Println("Grade: C")
}
// If with initialization
if num := 42; num%2 == 0 {
fmt.Printf("%d is even\n", num)
}
}
Output:
You are an adult
Grade: B
42 is even
🔹 Loops
Go has only one loop keyword:
for
package main
import "fmt"
func main() {
// Traditional for loop
fmt.Println("Counting to 5:")
for i := 1; i <= 5; i++ {
fmt.Printf("%d ", i)
}
fmt.Println()
// While-style loop
fmt.Println("Countdown:")
count := 3
for count > 0 {
fmt.Printf("%d ", count)
count--
}
fmt.Println("Go!")
// Infinite loop (with break)
fmt.Println("Breaking at 3:")
for {
fmt.Printf("%d ", count)
count++
if count > 3 {
break
}
}
fmt.Println()
}
Output:
Counting to 5:
1 2 3 4 5
Countdown:
3 2 1 Go!
Breaking at 3:
0 1 2 3