Go syntax forms the foundation of writing code in this powerful and versatile language. Mastering it empowers you to express your programming ideas effectively and create efficient, readable applications.
Every Go program resides within a package, which organizes and groups related code. You’ll typically work with multiple packages, importing required functionality using the import
keyword.
package main
import (
"fmt" // For formatted printing
)
func main() {
fmt.Println("Hello, world!")
}
Variables store values during program execution. You declare them with a name, type (e.g., int
, string
, bool
), and optionally, an initial value:
var message string = "Welcome to Go!" // Explicit declaration
age := 30 // Implicit declaration with type inference
Constants represent fixed values that cannot change during the program’s lifetime. Declare them using const
:
const PI float64 = 3.14159 // Numeric constant
const MaxConnections int = 100 // Integer constant
Go offers fundamental data types like integers, floating-point numbers, strings, Booleans, and composite types like arrays, slices, maps, and structures. Each type has specific properties and operations:
// Numeric types: int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64
var num int = 42
var decimal float64 = 3.14
// String type: string
var name string = "Alice"
// Boolean type: bool
var isTrue bool = true
// Arrays: fixed-size, ordered collections of elements of the same type
var numbers [5]int = [5]int{1, 2, 3, 4, 5}
// Slices: dynamic-length, mutable sequences of elements
var fruits []string = []string{"apple", "banana", "orange"}
// Maps: unordered collections of key-value pairs
var ages map[string]int = map[string]int{"Alice": 30, "Bob": 25}
// Structures: user-defined composite types combining different data types
type Person struct {
Name string
Age int
}
var alice Person = Person{Name: "Alice", Age: 30}
Operators perform operations on values according to their types and precedence. Go provides arithmetic, logical, comparison, bitwise, assignment, and other operators
// Arithmetic operators: +, -, *, /, %, ++, --
sum := 10 + 20
product := 5 * 3
// Logical operators: &&, ||, !
isLoggedIn := username == "alice" && password == "secret"
// Comparison operators: ==, !=, <, >, <=, >=
age >= 18
// Assignment operators: =, +=, -=, *=, /=, etc.
var count int = 0
count += 1
// Bitwise operators: &, |, ^, <<, >>
flags := 0x01 | 0x02 // Combine bit flags
Control flow statements dictate how a program’s execution progresses, allowing decision-making and loops
if age >= 18 {
fmt.Println("You are eligible to vote.")
} else {
fmt.Println("You are not eligible to vote.")
}
for i := 0; i < 5; i++ {
fmt.Println(i)
}
for message := range messages {
fmt.Println(message) // Process messages as they arrive
}
var ptr *int // Pointer declaration
var num int = 10
ptr = &num // Assigning address of num to ptr
func functionName(parameterList) returnType {
// Function body
}
func add(a, b int) int {
return a + b
}
func (receiverType) methodName(parameterList) returnType {
// Method body
}
func (p *Person) sayHello() {
fmt.Println("Hello, my name is", p.Name)
}
type InterfaceName interface {
methodName() returnType
// Other methods...
}
type Shape interface {
area() float64
}
type StructName struct {
field1 dataType1
field2 dataType2
// Other fields...
}
type Person struct {
Name string
Age int
}
var arr [5]int // Array declaration
slice := make([]int, 5) // Slice creation
slice := []int{1, 2, 3} // Slice literal
Mastering Go syntax is essential for effective programming in the language. It provides the tools and structures necessary for organizing code, managing data, and controlling program flow. With packages, variables, constants, and various data types, along with operators and control flow statements, developers can create robust and efficient applications in Go. Happy coding !❤️