💻 Programming

Go

Golang

A systems language developed by Google. Supports concurrency with goroutines. Used in Docker and K8s.

📖 Detailed Explanation

Go (Golang) is a statically typed, compiled language developed in 2009 by Robert Griesemer, Rob Pike, and Ken Thompson at Google. It began from dissatisfaction with the complexity and long compile times of C++, and took simplicity and efficiency as its core design philosophy. Go is a language that "does more with less," allowing you to write code that is easy to learn and easy to read.

Go's most powerful feature is concurrent programming through goroutines and channels. Goroutines are much lighter than OS threads, so tens of thousands of them can run at the same time, and channels let goroutines exchange data safely with one another. Go's philosophy, "Do not communicate by sharing memory; instead, share memory by communicating," illustrates this well.

Go provides garbage collection while also compiling to native binaries, delivering excellent runtime performance. Cross-compilation is built in, so you can easily build executables for Linux, Windows, and macOS from a single codebase. Compilation is very fast, so even large projects build in a matter of seconds.

In practice, Go is used in key tools of the cloud-native ecosystem such as Docker, Kubernetes, Terraform, and etcd. It has particular strengths in developing microservices, API servers, CLI tools, and DevOps infrastructure, and it is widely adopted by large-scale services such as Netflix, Uber, Dropbox, and Cloudflare.

💻 Code Example

package main

import (
    "fmt"
    "net/http"
    "sync"
    "time"
)

// 1. 기본 HTTP 서버
func helloHandler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "안녕하세요! 현재 시간: %s", time.Now().Format("15:04:05"))
}

// 2. 고루틴과 채널을 활용한 동시성 처리
func fetchURLs(urls []string) map[string]int {
    results := make(map[string]int)
    ch := make(chan struct{ url string; status int })
    var wg sync.WaitGroup

    for _, url := range urls {
        wg.Add(1)
        go func(u string) {
            defer wg.Done()
            resp, err := http.Get(u)
            if err != nil {
                ch <- struct{ url string; status int }{u, 0}
                return
            }
            defer resp.Body.Close()
            ch <- struct{ url string; status int }{u, resp.StatusCode}
        }(url)
    }

    go func() {
        wg.Wait()
        close(ch)
    }()

    for result := range ch {
        results[result.url] = result.status
    }
    return results
}

// 3. 구조체와 메서드
type User struct {
    ID   int
    Name string
}

func (u *User) Greet() string {
    return fmt.Sprintf("안녕하세요, %s님!", u.Name)
}

func main() {
    // HTTP 서버 시작
    http.HandleFunc("/", helloHandler)
    fmt.Println("서버 시작: http://localhost:8080")
    http.ListenAndServe(":8080", nil)
}

🗣️ Say It Like This in Practice

💬 In a Meeting
"I think we should develop this microservice in Go. Goroutines let us efficiently handle thousands of concurrent requests, and since deployment is simple with a single binary, we can even shrink the Docker image to under 10MB."
💬 In an Interview
"In Go's concurrency model, goroutines use M:N scheduling. Multiple goroutines are multiplexed onto OS threads, so the context-switching cost is low. Communicating through channels makes it easier to prevent data races than using mutexes, and the select statement lets you wait on multiple channels at the same time."
💬 In a code review
"The channel created in this goroutine is never closed, so a goroutine leak may occur. Please fix it by adding close(ch) with defer, or by using context to propagate a cancellation signal."

Take a mock interview on this topic → WIA SOOM AI Interview · Start for free

⚠️ Common Mistakes & Cautions

❌
Ignoring Errors

Go returns error values instead of exceptions. If you ignore an error, as in `result, _ := someFunc()`, unexpected problems will occur at runtime. Always perform an `if err != nil` check.

❌
Goroutine Leaks

A goroutine that waits forever on a channel or runs without a termination condition causes a memory leak. Use context.WithCancel or context.WithTimeout to manage the goroutine's lifecycle.

✅
Go Modules and Dependency Management

Initialize a module with go mod init, and tidy up dependencies with go mod tidy. Include the go.sum file in version control to ensure reproducible builds.

🔗 Related Terms

📚 Learn More