Go logo

Go Beginner

Statically typed compiled language — minimalist syntax, structural typing, and high-performance concurrency models.

1 Entry Point & Variables

Every execution file must declare its package context. Go utilizes explicit static type mapping or the short assignment operator (:=) for local type inference.

go · main.go
package main

import "fmt"

func main() {
    // Short assignment syntax (declares + infers automatically)
    hostUrl := "https://vidux.sh"
    
    var port int = 8080 // Explicit standard declaration
    
    fmt.Println("Server running on:", hostUrl, port)
}
The Go compiler is uncompromising: **any declared variable or imported package that is not used will throw a compilation error.** No dead code allowed.

2 Data Types & Zero Values

Variables declared without an explicit initial value are automatically assigned their type-specific **Zero Value**, avoiding garbage memory pointers.

int / uint Platform dependent sizes Zero value: 0
float32 / float64 Explicit precision floats Zero value: 0.0
bool Logical true / false truths Zero value: false
string Immutable byte sequences Zero value: ""
slice []T Dynamic wrapper for arrays Zero value: nil
map[K]V Hash map key-value dictionary Zero value: nil

3 Conditionals & Inlines

Conditionals do not wrap statements inside parentheses. They also support a compact initialization statement scope execution path prior to evaluating conditions.

go · logic.go
if score := getMetric(); score > 90 {
    fmt.Println("Optimized pipeline execution")
} else if score >= 50 {
    fmt.Println("Stable system bounds")
} else {
    fmt.Println("Alert: threshold drop")
}
// 'score' is entirely out of scope here! Safely contained within the block.

4 The Only Loop: For

To avoid keyword overhead, Go leverages the for keyword to handle all looping behaviors, including standard loops, conditional while structures, and endless cycles.

go · loops.go
// 1. Classical initialization, condition, post-iteration
for i := 0; i < 3; i++ {
    fmt.Println(i)
}

// 2. Evaluated condition loop (Replaces standard 'while' structures)
n := 1
for n < 5 {
    n *= 2
}

// 3. Collections traversal using the range generator unpack loop
items := []string{"db", "cache"}
for index, value := range items {
    fmt.Printf("Idx: %d Val: %s\n", index, value)
}

5 Functions & Multi-Returns

Functions support passing multiple values as a tuple, a paradigm heavily integrated across the standard library for idiomatic error handling.

go · functions.go
// Parameters group right-aligned types; returns are bundled at the end
func divide(a, b float64) (float64, error) {
    if b == 0 {
        return 0, fmt.Errorf("division by zero error trap")
    }
    return a / b, nil
}

func main() {
    result, err := divide(10.0, 0.0)
    if err != nil {
        fmt.Println("Operation failed:", err)
        return
    }
    fmt.Println("Success mathematical operation result:", result)
}

6 Pointers & Structs

Go lacks object-oriented class definitions. Custom data structures are defined using struct schemas, and behavior is attached using explicit method receivers.

go · objects.go
type Repository struct {
    Name  string
    Stars int
}

// Pointer receiver method (*). Allows mutations to the original caller memory
func (r *Repository) AddStar() {
    r.Stars++ // Automatically dereferenced by Go syntax engine sugar
}

func main() {
    repo := Repository{Name: "ScratchMap", Stars: 42}
    repo.AddStar() // original instance updated directly via reference pointer passing
}
Go has **no pointer arithmetic**. You can fetch memory addresses via the & operator and read values using *, but you cannot manually shift pointer offsets.

7 Implicit Interfaces

Interfaces are satisfied **implicitly**. A concrete type does not need to explicitly implement an interface via a keyword; it simply needs to implement the matching method signatures.

go · interfaces.go
type Speaker interface {
    Speak() string
}

type Developer struct{}

// Satisfies Speaker protocol naturally without implement signatures declarations
func (d Developer) Speak() string {
    return "Compile routines completed successfully."
}

8 Goroutines & Channels

Concurrency is baked into the language core. Prefixing a call with go executes it on an ultra-lightweight thread, while **Channels** orchestrate data transfers between them.

go · concurrency.go
func fetchWorker(ch chan string) {
    ch <- "Data payload arrived securely!" // Send data into channel pipeline
}

func main() {
    // Allocate a synchronous communication channel pipeline string pipeline
    dataStream := make(chan string)
    
    go fetchWorker(dataStream) // Launches asynchronous parallel task sequence
    
    msg := <-dataStream        // Halts execution safely until worker resolves message
    fmt.Println(msg)
}
With Go fundamentals mastered, you are ready to construct scalable cloud infrastructures, microservices, and fast APIs using **Gin** or **Fiber**.