Skip to content

Instantly share code, notes, and snippets.

@1UC1F3R616
Last active April 13, 2024 19:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1UC1F3R616/d238ce9ba1ee6fc628312066f1281982 to your computer and use it in GitHub Desktop.
Save 1UC1F3R616/d238ce9ba1ee6fc628312066f1281982 to your computer and use it in GitHub Desktop.
Yet Another Go Cheat Sheet
Framework/Library Description Use Cases ORM Support Popularity (out of 5)
net/http Part of the Go standard library, provides HTTP client and server implementations. Building HTTP servers with minimal dependencies. No (Use standalone ORMs like GORM, SQLBoiler, or Ent) 5
Gin A HTTP web framework that features a Martini-like API but with better performance. Rapid development of RESTful APIs and web applications with performance and efficiency. No (Popular with GORM) 5
Echo A high performance, extensible, minimalist Go web framework. Creating high performance, extensible web applications and APIs with a focus on simplicity. No (Commonly used with GORM) 4.5
Beego An open-source, high-performance web framework for the Go programming language. Full-stack applications with MVC architecture, ORM, caching, and more, with less boilerplate. Yes (Built-in ORM) 4
Fiber Inspired by Express.js, designed to ease things up for fast development with zero memory allocation and performance in mind. Building fast and scalable web applications with a minimalistic approach similar to Express.js. No (Frequently used with GORM) 4.5
Gorilla Mux A powerful URL router and dispatcher for golang. Handling complex routing scenarios in web applications. No (Use standalone ORMs) 4
Chi A lightweight, idiomatic and composable router for building Go HTTP services. Building modular and composable web services with middleware support. No (Use standalone ORMs) 4
Buffalo Aims to make the life of a Go web developer easier. Full-stack web development with integrated tools for frontend, migrations, and more. Yes (Popular with Pop ORM, now maintained separately) 3.5
Gokit A programming toolkit for building microservices in Go. Building microservices with a focus on maintainability and robustness. No (Designed for interoperability with any ORM) 3.5
Gqlgen A GraphQL server library for Go that focuses on code generation. Building GraphQL APIs with strong typing, code generation, and customizability. No (Can integrate with any ORM) 4

General Go Interview Questions

  1. What is a goroutine in Go?

    • A goroutine is a lightweight thread managed by the Go runtime, used for executing functions concurrently. Unlike traditional threads, goroutines have low memory overhead. Start a goroutine using the go keyword.
      go func() {
          fmt.Println("Inside goroutine")
      }()
      fmt.Println("Outside goroutine")
  2. How do you manage package versions in Go?

    • Go uses modules for package management, allowing you to track versions via the go.mod file. You can specify versions of dependencies required by your application.
      go mod init example.com/mymodule
      go get github.com/example/lib@v1.2.3
  3. What is the difference between an array and a slice in Go?

    • Arrays have a fixed size, while slices are dynamic. Slices are more flexible and commonly used because they can grow and shrink as needed.
      var myArray [3]int
      mySlice := make([]int, 0)
      mySlice = append(mySlice, 1)
  4. Explain the use of interfaces in Go.

    • Interfaces allow you to define behavior. Any type that implements the interface's methods can be used wherever the interface is accepted.
      type Greeter interface {
          Greet() string
      }
      type Person struct {
          Name string
      }
      func (p Person) Greet() string {
          return "Hello, " + p.Name
      }
      func greetSomeone(g Greeter) {
          fmt.Println(g.Greet())
      }
  5. What are channels and how are they used?

    • Channels are used for communication between goroutines, acting as conduits for data transfer.
      ch := make(chan int)
      go func() {
          ch <- 1  // Sending data into channel
      }()
      val := <-ch // Receiving data from channel
      fmt.Println(val)
  6. How does Go handle memory management?

    • Go handles memory with an automatic garbage collector, which deallocates memory that is no longer in use, helping to avoid common memory leaks.
      // Memory management is handled automatically; example shows dynamic slice allocation
      slice := make([]int, 0)
      for i := 0; i < 1000; i++ {
          slice = append(slice, i)
      }
  7. What is a map in Go?

    • Maps are key-value stores used for fast lookup of data. Keys are unique.
      myMap := make(map[string]int)
      myMap["key"] = 42
      fmt.Println(myMap["key"])
  8. What are some ways to optimize performance in Go programs?

    • Use pprof for profiling, use goroutines for concurrency, and select efficient data structures.
      // Example of using goroutines for concurrency
      func process(data []int, result chan<- int) {
          sum := 0
          for _, num := range data {
              sum += num
          }
          result <- sum
      }
  9. Explain defer, panic, and recover in Go.

    • Use defer for cleanup, panic to handle unexpected errors, and recover to regain control after a panic.
      func riskyFunction() {
          defer func() {
              if r := recover(); r != nil {
                  fmt.Println("Recovered from:", r)
              }
          }()
          panic("something bad happened")
      }
  10. What is the zero value in Go, and why is it useful?

    • The zero value is the default value of a variable when no initial value is provided. It ensures type safety and predictable behavior.
      var i int    // 0
      var f float64 // 0.0
      var b bool   // false
      var s string // ""
      fmt.Println(i, f, b, s)

Go Backend Interview Questions

  1. What is the difference between a pointer and a value receiver in methods?

    • Pointer receivers can modify the underlying data, while value receivers work with a copy.
      type MyType struct {
          value int
      }
      func (m *MyType) Modify() {
          m.value = 10  // Modifies the receiver's data
      }
      func (m MyType) Display() {
          fmt.Println(m.value)
      }
  2. How do you handle errors in Go?

    • Return errors from functions and handle them explicitly in your code.
      func mightFail() (int, error) {
          return 0, errors.New("failed")
      }
      result, err := mightFail()
      if err != nil {
          log.Fatalf("Error: %v", err)
      }
      fmt.Println("Result:", result)
  3. What is a channel and how can you prevent it from leaking?

    • Ensure you close channels and properly synchronize goroutines to prevent leaks.
      ch := make(chan int)
      go func() {
          ch <- 1
          close(ch)
      }()
      for num := range ch {
          fmt.Println(num)
      }
  4. Explain how you would use middleware in a Go web server.

    • Middleware intercepts HTTP requests for logging, authentication, etc.
      func loggingMiddleware(next http.Handler) http.Handler {
          return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
              log.Printf("Request received: %s", r.URL.Path)
              next.ServeHTTP(w, r)
          })
      }
      http.Handle("/", loggingMiddleware(http.HandlerFunc(myHandler)))
  5. Describe how you would set up a connection to a SQL database in Go.

    • Use the database/sql package with appropriate database drivers.
      db, err := sql.Open("postgres", "connection_string_here")
      if err != nil {
          log.Fatal(err)
      }
      defer db.Close()
  6. What is context in Go, and how is it used?

    • Manage request-scoped values, cancellation signals, and deadlines.
      func operation(ctx context.Context) error {
          select {
          case <-ctx.Done():
              return ctx.Err()
          case <-time.After(1 * time.Hour):
              // operation logic here
          }
          return nil
      }
  7. How do you implement rate limiting in Go?

    • Use the golang.org/x/time/rate package to control the rate of operations.
      limiter := rate.NewLimiter(1, 5) // 1 request per second with a burst of 5
      if limiter.Allow() {
          // process request
      } else {
          // deny request
      }
  8. What are the benefits of using Go modules?

    • Dependency management, reproducible builds, and elimination of GOPATH.
      go mod init mymodule
      go mod tidy
  9. How does Go handle Unicode and how can you manipulate Unicode strings?

    • Use the unicode and strings packages for Unicode support.
      for _, rune := range "Hello, 世界" {
          fmt.Printf("%c ", rune)
      }
  10. How do you ensure that a Go application is concurrently safe?

    • Use mutexes, atomic operations, and proper channel usage to manage shared resources.
      var lock sync.Mutex
      var count int
      func increment() {
          lock.Lock()
          count++
          lock.Unlock()
      }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment