context 패키지는 취소 신호, 마감 시간(deadline), 요청 범위 값을 API 경계와 goroutine 전반에 전달합니다. 연산의 수명을 제어하는 표준 메커니즘으로 — 특히 작업을 취소하고 함수 호출 체인 전반에 타임아웃을 전파하는 데 쓰입니다.
해결하는 핵심 문제
하나의 요청이 여러 goroutine/함수에 걸쳐 작업을 시작(DB 쿼리, API 호출 등).
클라이언트가 연결을 끊거나 타임아웃이 걸리면, 그 모든 작업을 취소하여
goroutine이 쓸데없이 계속 실행되지 않게(자원 낭비/누수) 하고 싶음.
→ context가 "지금 멈춰" 신호를 전체 호출 체인에 전파.
context 생성
ctx := context.Background()
ctx := context.TODO()
ctx, cancel := context.WithTimeout(context.Background(), *time.Second)
cancel()
ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithDeadline(ctx, time.Now().Add(time.Minute))
