🐹

Go Essentials

49 cards · 🇺🇸 English

Core Go syntax: goroutines, channels, slices, and error handling.

Study all 49 cards for free

Flashcards, quizzes, pixel-art games and a daily review that remembers for you.

Study this deck free

Try the first 10 cards

1 / 10
Question

Go and who created it

Knew itLearning

Tap the card for the answer · swipe right if you knew it, left if not

Already have an account? Sign in

Cards

  1. 1Go and who created it
    Go (Golang) is a statically typed, compiled language created by Google (Robert Griesemer, Rob Pike, Ken Thompson) in 2009. Designed for simplicity, concurrency, and fast compilation
  2. 2Write a basic Hello World program in Go
    package main import "fmt" func main() { fmt.Println("Hello, World!") } Run: go run main.go Build: go build main.go
  3. 3Declare variables in Go
    var name string = "John" // explicit type var age = 25 // type inference count := 10 // short declaration (inside functions only) var x, y int = 1, 2 // multiple
  4. 4Basic data types in Go
    Numeric: int, int8/16/32/64, uint, uint8/16/32/64, float32/64, complex64/128 String: string Boolean: bool Byte: byte (alias for uint8) Rune: rune (alias for int32, Unicode)
  5. 5:= in Go
    Short variable declaration - declares and initializes in one line: x := 10 // equivalent to var x int = 10 Only works inside functions, infers type automatically
  6. 6Define a function in Go
    func add(a int, b int) int { return a + b } // Shortened: func add(a, b int) int { return a + b } // Multiple returns: func swap(a, b int) (int, int) { return b, a }
  7. 7Named return values in Go
    func divide(a, b float64) (result float64, err error) { if b == 0 { err = errors.New("division by zero") return } result = a / b return // naked return }
  8. 8Goroutine
    Lightweight thread managed by Go runtime. Start with 'go' keyword: go myFunction() go func() { // anonymous goroutine }() Thousands can run concurrently with minimal overhead
  9. 9Channel in Go
    Communication mechanism between goroutines: ch := make(chan int) // create ch <- 42 // send val := <-ch // receive close(ch) // close Channels are typed and blocking by default
  10. 10buffered vs unbuffered channels
    Unbuffered: ch := make(chan int) - send blocks until receive Buffered: ch := make(chan int, 10) - send blocks only when full Unbuffered = synchronous, Buffered = asynchronous
  11. 11Defer in Go
    Delays execution until surrounding function returns: defer file.Close() defer fmt.Println("World") fmt.Println("Hello") // Prints: Hello World (LIFO order) Used for cleanup
  12. 12Panic and recover in Go
    panic: stops normal execution, unwinds stack panic("something went wrong") recover: catches panic (only in deferred functions) defer func() { if r := recover(); r != nil { fmt.Println("Recovered:", r) } }() Like try-catch but for exceptional cases
  13. 13How does error handling work in Go?
    Functions return error as last value: func divide(a, b int) (int, error) { if b == 0 { return 0, errors.New("division by zero") } return a / b, nil } result, err := divide(10, 0) if err != nil { // handle error }
  14. 14Interface in Go
    Collection of method signatures: type Writer interface { Write([]byte) (int, error) } Implemented implicitly - no 'implements' keyword Any type with Write method satisfies Writer interface
  15. 15Empty interface
    interface{} (or any in Go 1.18+) - can hold values of any type: var val interface{} val = 42 val = "hello" val = []int{1, 2, 3} Like Object in Java or void* in C
  16. 16Create a struct in Go
    type Person struct { Name string Age int } // Initialize: p1 := Person{"John", 30} p2 := Person{Name: "Jane", Age: 25} p3 := new(Person) // pointer to zero value
  17. 17Define methods on structs
    func (p Person) Greet() string { return "Hello, " + p.Name } // Pointer receiver (can modify): func (p *Person) Birthday() { p.Age++ } p := Person{"John", 30} p.Greet() p.Birthday()
  18. 18pointer vs value receivers
    Value: func (p Person) - receives copy, cannot modify original Pointer: func (p *Person) - receives reference, can modify Use pointer when: modifying, avoiding copies of large structs
  19. 19Create and use a slice
    nums := []int{1, 2, 3} // slice literal slice := make([]int, 5) // length 5, capacity 5 slice := make([]int, 5, 10) // length 5, capacity 10 slice = append(slice, 6) // add element len(slice), cap(slice) // length and capacity
  20. 20array vs slice
    Array: fixed size, value type var arr [5]int arr := [3]int{1, 2, 3} Slice: dynamic size, reference to array slice := []int{1, 2, 3} slice = append(slice, 4) Slices are more common
  21. 21Create and use a map
    // Create: m := make(map[string]int) m := map[string]int{"age": 30} // Operations: m["name"] = 25 // set val := m["name"] // get val, ok := m["name"] // check existence delete(m, "name") // delete len(m) // size
  22. 22Range in Go
    Iterates over slices, arrays, maps, strings, channels: for i, val := range slice { } // index, value for key, val := range m { } // map for i := range slice { } // index only for _, val := range slice { } // value only (ignore index)
  23. 23Select statement
    Waits on multiple channel operations: select { case msg := <-ch1: fmt.Println(msg) case msg := <-ch2: fmt.Println(msg) case <-time.After(1 * time.Second): fmt.Println("timeout") default: fmt.Println("no activity") }
  24. 24Go's approach to inheritance
    No inheritance - uses composition: type Address struct { City string } type Person struct { Name string Address // embedded } p := Person{Name: "John", Address: Address{"NYC"}} p.City // direct access to embedded field
  25. 25Go modules
    Dependency management system (since Go 1.11): go mod init myproject // initialize go get package@version // add dependency go mod tidy // clean up go.mod file tracks dependencies
  26. 26Handle concurrency safely
    1. Channels: communicate data between goroutines 2. Mutex: sync.Mutex for protecting shared data var mu sync.Mutex mu.Lock() shared++ mu.Unlock() 3. sync.WaitGroup: wait for goroutines to finish
  27. 27Sync.WaitGroup
    Waits for collection of goroutines to finish: var wg sync.WaitGroup for i := 0; i < 5; i++ { wg.Add(1) go func() { defer wg.Done() // work }() } wg.Wait() // block until all Done()
  28. 28Context in Go
    Carries deadlines, cancellation signals, request-scoped values: ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() select { case <-ctx.Done(): return ctx.Err() // work } Essential for request handling, cancellation
  29. 29Pointers in Go
    var p *int // pointer to int x := 42 p = &x // address of x fmt.Println(*p) // dereference (value at address) *p = 100 // modify through pointer No pointer arithmetic (unlike C)
  30. 30Init function
    Special function called before main: func init() { // initialization code } Multiple init functions allowed (executed in order) Used for package initialization

And 19 more cards. Study the whole deck free

Articles about this deck

More Programming decks

See all
Study all 49 cards free