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.
Try the first 10 cards
1 / 10Go and who created it
Tap the card for the answer · swipe right if you knew it, left if not
Already have an account? Sign in
Cards
- 1Go and who created itGo (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
- 2Write a basic Hello World program in Gopackage main import "fmt" func main() { fmt.Println("Hello, World!") } Run: go run main.go Build: go build main.go
- 3Declare variables in Govar name string = "John" // explicit type var age = 25 // type inference count := 10 // short declaration (inside functions only) var x, y int = 1, 2 // multiple
- 4Basic data types in GoNumeric: 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:= in GoShort variable declaration - declares and initializes in one line: x := 10 // equivalent to var x int = 10 Only works inside functions, infers type automatically
- 6Define a function in Gofunc 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 }
- 7Named return values in Gofunc divide(a, b float64) (result float64, err error) { if b == 0 { err = errors.New("division by zero") return } result = a / b return // naked return }
- 8GoroutineLightweight thread managed by Go runtime. Start with 'go' keyword: go myFunction() go func() { // anonymous goroutine }() Thousands can run concurrently with minimal overhead
- 9Channel in GoCommunication mechanism between goroutines: ch := make(chan int) // create ch <- 42 // send val := <-ch // receive close(ch) // close Channels are typed and blocking by default
- 10buffered vs unbuffered channelsUnbuffered: ch := make(chan int) - send blocks until receive Buffered: ch := make(chan int, 10) - send blocks only when full Unbuffered = synchronous, Buffered = asynchronous
- 11Defer in GoDelays execution until surrounding function returns: defer file.Close() defer fmt.Println("World") fmt.Println("Hello") // Prints: Hello World (LIFO order) Used for cleanup
- 12Panic and recover in Gopanic: 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
- 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 }
- 14Interface in GoCollection of method signatures: type Writer interface { Write([]byte) (int, error) } Implemented implicitly - no 'implements' keyword Any type with Write method satisfies Writer interface
- 15Empty interfaceinterface{} (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
- 16Create a struct in Gotype Person struct { Name string Age int } // Initialize: p1 := Person{"John", 30} p2 := Person{Name: "Jane", Age: 25} p3 := new(Person) // pointer to zero value
- 17Define methods on structsfunc (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()
- 18pointer vs value receiversValue: 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
- 19Create and use a slicenums := []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
- 20array vs sliceArray: 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
- 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
- 22Range in GoIterates 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)
- 23Select statementWaits 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") }
- 24Go's approach to inheritanceNo 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
- 25Go modulesDependency 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
- 26Handle concurrency safely1. 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
- 27Sync.WaitGroupWaits 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()
- 28Context in GoCarries 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
- 29Pointers in Govar 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)
- 30Init functionSpecial 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- 🐦
Swift Essentials
48 cards · 🇺🇸 English
Core Swift syntax, optionals, and iOS-friendly language features.
- 🎯
Kotlin Essentials
42 cards · 🇺🇸 English
Core Kotlin syntax, null safety, and Android-friendly language features.
- 🦀
Rust Essentials
48 cards · 🇺🇸 English
Ownership, borrowing, and core Rust syntax.
- 🧱
Data Structures
95 cards · 🇺🇸 English
Arrays, linked lists, trees, graphs, hash tables and their core operations.
- 🟣
C#: Fundamentals & .NET Core
99 cards · 🇺🇸 English
Core C# syntax, types, and .NET Core concepts in one deck.
- 🌐
APIs: REST, GraphQL & HTTP Status Codes
108 cards · 🇺🇸 English
REST and GraphQL API concepts plus common HTTP status codes.
