Skip to content

Latest commit

 

History

History
270 lines (196 loc) · 4.26 KB

File metadata and controls

270 lines (196 loc) · 4.26 KB

Summary of Go (Golang) Documentation

Overview

Go, or Golang, is an open-source programming language designed by Google. It is statically typed, compiled, and known for its simplicity, performance, and efficient concurrency handling. Go is widely used for building scalable and high-performance applications.


Key Features

  • Simple Syntax: Designed for readability and ease of use.
  • Statically Typed: Strong type-checking at compile time.
  • Garbage Collection: Automatic memory management.
  • Concurrency Support: Built-in support for concurrent programming with goroutines and channels.
  • Fast Compilation: Compiles quickly to native machine code.
  • Standard Library: Rich library for networking, file handling, and other utilities.

Core Concepts

Syntax Basics

  • Hello World:

    package main
    
    import "fmt"
    
    func main() {
        fmt.Println("Hello, Go!")
    }
  • Comments:

    // Single-line comment
    /*
       Multi-line
       comment
    */

Variables

  • Declaration with var or shorthand (:=):

    var name string = "Go"
    age := 10 // shorthand
  • Constants:

    const Pi = 3.14

Data Types

  • Basic Types:
    • int, float64, string, bool
  • Composite Types:
    • array, slice, map, struct
  • Special Types:
    • interface, chan, func

Control Structures

Conditionals

if x > 0 {
    fmt.Println("Positive")
} else if x == 0 {
    fmt.Println("Zero")
} else {
    fmt.Println("Negative")
}

Loops

  • For Loop (the only loop in Go):

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

    numbers := []int{1, 2, 3}
    for i, num := range numbers {
        fmt.Println(i, num)
    }

Functions

  • Definition:

    func add(a int, b int) int {
        return a + b
    }
  • Multiple Return Values:

    func divide(a, b int) (int, int) {
        return a / b, a % b
    }
  • Anonymous Functions:

    func() {
        fmt.Println("Anonymous function!")
    }()

Pointers

  • Direct memory address manipulation:
    var x int = 10
    var ptr *int = &x
    fmt.Println(*ptr) // Dereferences the pointer

Structs and Methods

  • Structs:

    type Person struct {
        Name string
        Age  int
    }
    
    p := Person{Name: "Alice", Age: 30}
    fmt.Println(p.Name)
  • Methods:

    func (p Person) Greet() string {
        return "Hello, " + p.Name
    }

Interfaces

  • Define behavior for types:
    type Greeter interface {
        Greet() string
    }

Concurrency

Goroutines

  • Lightweight thread managed by the Go runtime:
    go func() {
        fmt.Println("Concurrent execution")
    }()

Channels

  • Communication mechanism between goroutines:
    ch := make(chan int)
    go func() { ch <- 42 }()
    fmt.Println(<-ch)

Error Handling

  • Go uses explicit error handling:
    result, err := divide(10, 0)
    if err != nil {
        fmt.Println("Error:", err)
    }

File Handling

  • Reading from a File:

    data, err := os.ReadFile("example.txt")
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(string(data))
  • Writing to a File:

    err := os.WriteFile("example.txt", []byte("Hello, Go!"), 0644)
    if err != nil {
        log.Fatal(err)
    }

Package Management

  • Organize code into packages:

    package mathutils
    
    func Add(a, b int) int {
        return a + b
    }
  • Importing packages:

    import "mathutils"
    result := mathutils.Add(3, 5)

Best Practices

  • Use clear and concise variable names.
  • Leverage Go’s strong typing for safety.
  • Prefer idiomatic Go patterns (e.g., error handling).
  • Optimize for simplicity and readability.
  • Use go fmt to format code consistently.

Resources

Go’s simplicity, performance, and excellent concurrency support make it an ideal choice for modern, high-performance applications.