Skip to content

Go

Go (Golang) Syntax Breakdown & Advanced Features

Go is a statically typed, compiled language known for simplicity, concurrency, and performance. Let's break it down:


1. Basic Syntax Breakdown

Hello World

package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
}
  • package main: Defines an executable package.
  • import "fmt": Imports a package (fmt for formatted I/O).
  • func main(): Entry point of execution.
  • fmt.Println(): Prints output to the console.

2. Variables and Constants

Variable Declaration

var name string = "Go"
var age int = 10

Short Declaration (Only inside functions)

name := "Go"
age := 10

Constants

const Pi = 3.14

3. Data Types

Type Example
string "Hello"
int, int64 42, 9223372036854775807
float32, float64 3.14
bool true, false
array [3]int{1, 2, 3}
slice []int{1, 2, 3}
map map[string]int{"age": 30}

4. Control Flow

If-Else

if age > 18 {
    fmt.Println("Adult")
} else {
    fmt.Println("Minor")
}

For Loop

for i := 0; i < 5; i++ {
    fmt.Println(i)
}

Range Loop

arr := []string{"Go", "Python", "Rust"}
for index, value := range arr {
    fmt.Println(index, value)
}

Switch

num := 2
switch num {
case 1:
    fmt.Println("One")
case 2:
    fmt.Println("Two")
default:
    fmt.Println("Other")
}

5. Functions

func add(a int, b int) int {
    return a + b
}

Multiple Returns

func swap(a, b string) (string, string) {
    return b, a
}

6. Structs (Similar to Classes)

type Person struct {
    Name string
    Age  int
}

func main() {
    p := Person{Name: "Alice", Age: 25}
    fmt.Println(p.Name, p.Age)
}

7. Pointers

x := 5
p := &x // Pointer to x
fmt.Println(*p) // Dereferencing

8. Maps (Key-Value Pairs)

ages := map[string]int{"Alice": 25, "Bob": 30}
ages["Charlie"] = 35
fmt.Println(ages)

Advanced Features

1. Goroutines (Concurrency)

  • Run functions concurrently using go keyword.
package main

import (
    "fmt"
    "time"
)

func sayHello() {
    fmt.Println("Hello from Goroutine!")
}

func main() {
    go sayHello() // Runs in the background
    time.Sleep(time.Second) // Prevents program from exiting immediately
}

2. Channels (Thread Communication)

package main

import "fmt"

func sendMessage(ch chan string) {
    ch <- "Hello from Goroutine!"
}

func main() {
    ch := make(chan string)
    go sendMessage(ch)
    fmt.Println(<-ch) // Receives message
}

3. Select (Multiple Channel Handling)

package main

import "fmt"

func main() {
    ch1 := make(chan string)
    ch2 := make(chan string)

    go func() { ch1 <- "From Channel 1" }()
    go func() { ch2 <- "From Channel 2" }()

    select {
    case msg1 := <-ch1:
        fmt.Println(msg1)
    case msg2 := <-ch2:
        fmt.Println(msg2)
    }
}

4. Interfaces (Similar to Abstract Classes)

package main

import "fmt"

type Animal interface {
    Speak() string
}

type Dog struct{}

func (d Dog) Speak() string {
    return "Woof!"
}

func main() {
    var a Animal = Dog{}
    fmt.Println(a.Speak()) // Output: Woof!
}

5. Error Handling

package main

import (
    "errors"
    "fmt"
)

func divide(a, b int) (int, error) {
    if b == 0 {
        return 0, errors.New("cannot divide by zero")
    }
    return a / b, nil
}

func main() {
    result, err := divide(10, 0)
    if err != nil {
        fmt.Println("Error:", err)
    } else {
        fmt.Println("Result:", result)
    }
}

Short Setup Tutorial

1. Install Go

Verify installation:

go version

2. Set Up Go Workspace

mkdir go_project && cd go_project
go mod init example.com/go_project  # Initialize a Go module

3. Run a Go Program

Create main.go:

package main

import "fmt"

func main() {
    fmt.Println("Go is set up!")
}

Run it:

go run main.go

4. Build and Execute

go build -o myapp main.go
./myapp  # Run the compiled binary

5. Install Dependencies

go get github.com/gin-gonic/gin  # Example: Install Gin web framework

Conclusion

Go is simple yet powerful, with concurrency, memory efficiency, and robust error handling. 🚀