1. Go 언어의 핵심 철학 및 특징
Go(Golang)는 구글에서 개발한 정적 타입 컴파일 언어로, **간결함(Simplicity)**과 높은 생산성, 강력한 동시성 처리를 최우선 가치로 설계되었습니다.
- 간결한 문법: 키워드가 25개 내외로 적어 코드 읽기와 유지보수가 직관적입니다.
- 초고속 컴파일 및 실행 속도: C/C++에 육박하는 빌드 및 실행 성능을 제공합니다.
- Goroutine 기반 동시성 지원: 경량 스레드(Goroutine)를 통해 수만 개의 동시 작업을 적은 메모리로 처리합니다.
- Go 1.18+ 제네릭(Generics) 지원: 타입 안전성을 유지하면서 재사용 가능한 데이터 구조 작성이 가능합니다.
- Go 1.22+ 향상된
net/http라우팅: 별도 외부 프레임워크 없이 표준 라이브러리만으로 HTTP 메서드 및 경로 파라미터 캡처가 가능합니다.
2. Go 동시성(Concurrency) 핵심 패턴
Go는 “Don’t communicate by sharing memory; instead, share memory by communicating.” (메모리를 공유하여 통신하지 말고, 통신함으로써 메모리를 공유하라)는 명언으로 대표되는 CSP(Communicating Sequential Processes) 모델을 따릅니다.
① Goroutine & Channel 기본 개념
package main
import (
"fmt"
"time"
)
func worker(id int, ch chan string) {
time.Sleep(time.Millisecond * 500)
ch <- fmt.Sprintf("Worker %d 작업 완료", id)
}
func main() {
ch := make(chan string)
// 3개의 고루틴 병렬 실행
for i := 1; i <= 3; i++ {
go worker(i, ch)
}
// 채널로부터 결과 수신
for i := 1; i <= 3; i++ {
msg := <-ch
fmt.Println(msg)
}
}
② Worker Pool 패턴 (실무 추천 패턴)
대량의 작업 요청을 제한된 고루틴 스레드 풀에서 안전하게 처리하는 실무 패턴입니다.
package main
import (
"context"
"fmt"
"sync"
"time"
)
type Task struct {
ID int
}
func Worker(ctx context.Context, id int, tasks <-chan Task, results chan<- string, wg *sync.WaitGroup) {
defer wg.Done()
for {
select {
case <-ctx.Done():
return
case task, ok := <-tasks:
if !ok {
return
}
// 작업 처리 로직
time.Sleep(100 * time.Millisecond)
results <- fmt.Sprintf("[Worker %d] Task %d 처리 성공", id, task.ID)
}
}
}
func main() {
const numJobs = 10
const numWorkers = 3
tasks := make(chan Task, numJobs)
results := make(chan string, numJobs)
var wg sync.WaitGroup
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
// Worker Pool 생성
for w := 1; w <= numWorkers; w++ {
wg.Add(1)
go Worker(ctx, w, tasks, results, &wg)
}
// Task 투입
for j := 1; j <= numJobs; j++ {
tasks <- Task{ID: j}
}
close(tasks)
// 모든 Worker 종료 대기 후 결과 채널 닫기
go func() {
wg.Wait()
close(results)
}()
// 결과 수집
for res := range results {
fmt.Println(res)
}
}
3. Go 언어 활용 분야 및 결론
Go는 현재 **Cloud Native 생태계(Docker, Kubernetes, Terraform, Prometheus 등)**의 사실상 표준 언어로 자리 잡았습니다. 네트워크 서버, 대용량 트래픽 백엔드 API, Microservice Architecture(MSA) 서비스 구축 시 최선의 선택지입니다.