close
close
golang cheat sheet

golang cheat sheet

2 min read 17-10-2024
golang cheat sheet

Go (Golang) Cheat Sheet: A Quick Reference Guide

Go, also known as Golang, is a statically typed, compiled programming language designed by Google. It's known for its simplicity, efficiency, and concurrency features. This cheat sheet provides a quick overview of fundamental Go concepts and syntax, ideal for both beginners and seasoned developers seeking a handy reference.

Basic Syntax

  • Variables: Variables are declared using the var keyword and their type is specified after the variable name.
var name string = "John Doe"
age := 30 // Short variable declaration (type inferred)
  • Data Types: Go supports various data types, including integers (int, uint), floating-point numbers (float32, float64), strings (string), booleans (bool), and arrays (array).

  • Arrays and Slices: Arrays have a fixed size, while slices are dynamic, allowing for resizing.

numbers := [3]int{1, 2, 3} // Array with fixed size
fruits := []string{"apple", "banana"} // Slice
  • Functions: Functions are declared using the func keyword followed by the function name, parameters, and return types.
func add(x int, y int) int {
    return x + y
}

Control Flow

  • If-Else Statements: Used to execute different code blocks based on conditions.
if age >= 18 {
    fmt.Println("You are an adult")
} else {
    fmt.Println("You are a minor")
}
  • For Loops: Iterate over a range of values or until a condition is met.
for i := 0; i < 5; i++ {
    fmt.Println(i)
}
  • Switch Statements: Efficiently check for multiple conditions.
switch grade {
case "A":
    fmt.Println("Excellent")
case "B":
    fmt.Println("Good")
default:
    fmt.Println("Needs improvement")
}

Concurrency

  • Goroutines: Lightweight threads managed by the Go runtime, enabling concurrent execution.
go func() {
    fmt.Println("Running in a goroutine")
}()
  • Channels: Used for communication between goroutines, enabling safe data exchange.
ch := make(chan int)
go func() {
    ch <- 10 // Send data to channel
}()
value := <-ch // Receive data from channel

Pointers

  • Pointers: Store memory addresses of variables.
var num int = 10
var ptr *int = &num // ptr points to num

Structs

  • Structs: Custom data structures that group related variables.
type Person struct {
    name string
    age  int
}

Packages and Modules

  • Packages: Organize code into logical units.
  • Modules: Versioned packages that allow for dependency management.

Example: Simple Web Server

This example showcases a basic Go web server using the net/http package.

package main

import (
    "fmt"
    "net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello, world!")
}

func main() {
    http.HandleFunc("/", handler)
    http.ListenAndServe(":8080", nil)
}

Key Resources:

Conclusion:

This cheat sheet provides a starting point for learning and working with Go. The key to mastering Go lies in understanding its core principles of concurrency, simplicity, and efficiency. Remember to refer to the official Go documentation for further details and explore the vibrant Go community for support and inspiration.

Related Posts


Latest Posts