Go ๋์์ธ ํจํด: Go ๊ฐ๋ฐ์๋ฅผ ์ํ ํ์ ํจํด๊ณผ ๋ฉด์ ๋๋น ๊ฐ์ด๋
Go์ ์ค๊ณ ์ฒ ํ์ ๊ธฐ๋ฐํ ๋์์ธ ํจํด์ ์ฒด๊ณ์ ์ผ๋ก ํด์คํฉ๋๋ค. Functional Options, Strategy, Observer, Middleware ๋ฑ ๋ฉด์ ์์ ์์ฃผ ์ถ์ ๋๋ ์ค์ ํจํด๊ณผ ๋ต๋ณ ์ ๋ต์ ๋ค๋ฃน๋๋ค.

Go์ ๋์์ธ ํจํด์ ๊ฐ์ฒด์งํฅ ์ธ์ด์ ํจํด๊ณผ ๊ทผ๋ณธ์ ์ผ๋ก ๋ค๋ฆ ๋๋ค. ํด๋์ค๋ ์์ ์์ด Go๋ ์ปดํฌ์ง์ , ์ธํฐํ์ด์ค, ์ผ๊ธ ํจ์๋ฅผ ํ์ฉํ์ฌ ๋์ผํ ๊ตฌ์กฐ์ ์ ์ฐ์ฑ์ ๋ฌ์ฑํฉ๋๋ค. ํํ ์ ์ ์ฝ๋์ ๋ ๋์ ๋ช ํ์ฑ์ผ๋ก ์ด๋ฅผ ๊ตฌํํฉ๋๋ค.
Go๋ ์์๋ณด๋ค ์ปดํฌ์ง์ ์ ์ฐ์ ํฉ๋๋ค. ๋ชจ๋ ์ ํต์ ์ธ ๋์์ธ ํจํด์ ํด๋์ค ๊ณ์ธต ๊ตฌ์กฐ๊ฐ ์๋ ์ธํฐํ์ด์ค, ๊ตฌ์กฐ์ฒด ์๋ฒ ๋ฉ, ์ผ๊ธ ํจ์๋ฅผ ํ์ฉํ์ฌ ์ ์ฉํด์ผ ํฉ๋๋ค.
Functional Options ํจํด: ์ ์ฐํ ์ค์ ๊ตฌํ
Functional Options ํจํด์ Go์์ ์์ฃผ ๋ฐ์ํ๋ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํฉ๋๋ค. ๋ง์ ์ ํ์ ๋งค๊ฐ๋ณ์๋ฅผ ๊ฐ์ง ๊ฐ์ฒด๋ฅผ ์์ฑํ ๋, ๋ฉ์๋ ์ค๋ฒ๋ก๋ฉ์ด๋ ๊ธฐ๋ณธ ์ธ์๊ฐ ์๋ Go์์๋ ์ด๋ฅผ ์ฒ๋ฆฌํ๊ธฐ๊ฐ ๊น๋ค๋กญ์ต๋๋ค. Builder ํจํด๋ ๊ฐ๋ฅํ์ง๋ง ์ฅํฉํด์ง๊ธฐ ์ฝ์ต๋๋ค. Functional Options๋ ์๊ตฌ์ฌํญ์ด ์ฆ๊ฐํด๋ ํ์ ํธํ์ฑ์ ์ ์งํ๋ ๊น๋ํ๊ณ ํ์ฅ ๊ฐ๋ฅํ API๋ฅผ ์ ๊ณตํฉ๋๋ค.
์ด ํจํด์ Dave Cheney๊ฐ ๋์คํํ์ผ๋ฉฐ, ํ์ฌ Go ์ํ๊ณ ์ ๋ฐ์์ ํ์ค์ผ๋ก ์ฌ์ฉ๋ฉ๋๋ค. ๊ฐ๋ณ ์ธ์ ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ๊ตฌ์กฐ์ฒด๋ฅผ ์ค์ ํ๋ ๋ฐฉ์์ ๋๋ค.
package server
import (
"time"
"log/slog"
)
// Server holds the HTTP server configuration.
type Server struct {
host string
port int
timeout time.Duration
maxConns int
logger *slog.Logger
}
// Option defines a functional option for Server.
type Option func(*Server)
// WithPort sets the server port.
func WithPort(port int) Option {
return func(s *Server) {
s.port = port
}
}
// WithTimeout sets the request timeout.
func WithTimeout(d time.Duration) Option {
return func(s *Server) {
s.timeout = d
}
}
// WithMaxConns sets the maximum concurrent connections.
func WithMaxConns(n int) Option {
return func(s *Server) {
s.maxConns = n
}
}
// New creates a Server with sensible defaults and applies options.
func New(host string, opts ...Option) *Server {
srv := &Server{
host: host,
port: 8080, // default port
timeout: 30 * time.Second, // default timeout
maxConns: 100, // default max connections
logger: slog.Default(),
}
for _, opt := range opts {
opt(srv)
}
return srv
}ํธ์ถํ๋ ์ชฝ์์๋ ๊ธฐ๋ณธ๊ฐ๊ณผ ๋ค๋ฅธ ๋ถ๋ถ๋ง ์ง์ ํ๋ฉด ๋ฉ๋๋ค. ์๋ก์ด ์ต์
์ ์ถ๊ฐํด๋ ๊ธฐ์กด ํธ์ถ ์ฝ๋์ ์ํฅ์ ์ฃผ์ง ์์ต๋๋ค. ์ด ํจํด์ google.golang.org/grpc์ go.uber.org/zap ๊ฐ์ ํ๋ก๋์
๋ผ์ด๋ธ๋ฌ๋ฆฌ์์๋ ์ฌ์ฉ๋ฉ๋๋ค.
Strategy ํจํด: ์ธํฐํ์ด์ค ๊ธฐ๋ฐ ๋คํ์ฑ
Strategy ํจํด์ ๊ต์ฒด ๊ฐ๋ฅํ ์๊ณ ๋ฆฌ์ฆ์ ๊ณตํต ์ธํฐํ์ด์ค ๋ค์ ์บก์ํํฉ๋๋ค. Go์์๋ ์ถ์ ํด๋์ค๋ ์์ ์ฒด์ธ ์์ด ์ธํฐํ์ด์ค ๊ธฐ๋ฐ ๋คํ์ฑ์ผ๋ก ์ง์ ๊ตฌํํ ์ ์์ต๋๋ค.
๊ฒฐ์ ์ฒ๋ฆฌ ์์คํ ์ด ์ด ํจํด์ ์ข์ ์์์ ๋๋ค. ๊ฐ ๊ฒฐ์ ๋ฐฉ์์ด ๋์ผํ ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ๊ณ , ์ฒดํฌ์์ ์๋น์ค๊ฐ ๋ฐํ์์ ์ ๋ต์ ์ ํํฉ๋๋ค.
package payment
import "fmt"
// Processor defines the strategy interface.
type Processor interface {
Pay(amount float64) (string, error)
}
// CreditCard implements Processor for card payments.
type CreditCard struct {
CardNumber string
Expiry string
}
func (c *CreditCard) Pay(amount float64) (string, error) {
// Charge the card via payment gateway
return fmt.Sprintf("charged %.2f to card ending %s", amount, c.CardNumber[len(c.CardNumber)-4:]), nil
}
// BankTransfer implements Processor for wire transfers.
type BankTransfer struct {
IBAN string
}
func (b *BankTransfer) Pay(amount float64) (string, error) {
return fmt.Sprintf("initiated transfer of %.2f to %s", amount, b.IBAN), nil
}
// Checkout processes a payment using the given strategy.
func Checkout(p Processor, amount float64) error {
receipt, err := p.Pay(amount)
if err != nil {
return fmt.Errorf("payment failed: %w", err)
}
fmt.Println(receipt)
return nil
}Go ์ธํฐํ์ด์ค๋ ์๋ฌต์ ์ผ๋ก ์ถฉ์กฑ๋ฉ๋๋ค. implements ํค์๋๊ฐ ํ์ํ์ง ์์ต๋๋ค. Pay(float64) (string, error) ๋ฉ์๋๋ฅผ ๊ฐ์ง ๋ชจ๋ ํ์
์ด Processor๋ก ์ฌ์ฉ๋ ์ ์์ต๋๋ค. ์ด๋ก ์ธํด ๊ฒฐํฉ๋๊ฐ ๋ฎ์์ง๊ณ ํ
์คํธ๊ฐ ๊ฐํธํด์ง๋๋ค. ์์ธก ๊ฐ๋ฅํ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํํ๋ mock Processor๋ฅผ ์ ๋ฌํ๋ฉด ๋ฉ๋๋ค.
ํฉํ ๋ฆฌ ํจ์์ ์์ฑ์ ํจํด
Go์๋ ์์ฑ์๊ฐ ์์ต๋๋ค. ๊ด์ฉ์ ์ธ ๋์์ New ๋๋ NewXxx๋ก ๋ช
๋ช
๋ ํฉํ ๋ฆฌ ํจ์๋ก, ์ด๊ธฐํ๋ ๊ตฌ์กฐ์ฒด๋ฅผ ๋ฐํํฉ๋๋ค. ํฉํ ๋ฆฌ ํจ์๋ ์ ๋ก ๊ฐ ์ด๊ธฐํ๋ก๋ ๋ณด์ฅํ ์ ์๋ ๋ถ๋ณ ์กฐ๊ฑด์ ๊ฐ์ ํฉ๋๋ค.
๋ฉด์ ์ค๋น์์ ์ด ๊ตฌ๋ถ์ ์ค์ํฉ๋๋ค. ํฉํ ๋ฆฌ ํจ์๊ฐ ํ์ํ ๊ฒฝ์ฐ์ ์ ๋ก ๊ฐ์ด ๊ธฐ๋ณธ์ผ๋ก ์ ์ฉํ ๊ฒฝ์ฐ๋ฅผ ์ฌ๋ฐ๋ฅด๊ฒ ํ๋จํ๋ ๋ฅ๋ ฅ์ด ์๊ตฌ๋ฉ๋๋ค.
package pool
import (
"errors"
"sync"
)
// ConnPool manages a pool of reusable connections.
type ConnPool struct {
mu sync.Mutex
conns []Conn
maxSize int
}
// Conn represents a database connection.
type Conn struct {
ID int
Active bool
}
// NewConnPool validates parameters and returns an initialized pool.
func NewConnPool(maxSize int) (*ConnPool, error) {
if maxSize <= 0 {
return nil, errors.New("pool: maxSize must be positive")
}
return &ConnPool{
conns: make([]Conn, 0, maxSize),
maxSize: maxSize,
}, nil
}
// Acquire returns a connection from the pool.
func (p *ConnPool) Acquire() (*Conn, error) {
p.mu.Lock()
defer p.mu.Unlock()
for i := range p.conns {
if !p.conns[i].Active {
p.conns[i].Active = true
return &p.conns[i], nil
}
}
if len(p.conns) >= p.maxSize {
return nil, errors.New("pool: no available connections")
}
c := Conn{ID: len(p.conns) + 1, Active: true}
p.conns = append(p.conns, c)
return &p.conns[len(p.conns)-1], nil
}ํฉํ ๋ฆฌ ํจ์ NewConnPool์ maxSize ๋งค๊ฐ๋ณ์๋ฅผ ๊ฒ์ฆํ๊ณ ์ฌ๋ผ์ด์ค๋ฅผ ์ฌ์ ํ ๋นํฉ๋๋ค. ๊ตฌ์กฐ์ฒด๋ฅผ ์ง์ ์ด๊ธฐํ(&ConnPool{})ํ๋ฉด ๊ฒ์ฆ์ด ์๋ต๋์ด ๋ฐํ์ ํจ๋์ด ๋ฐ์ํ ์ ์์ต๋๋ค. ์ด ํจํด์ Functional Options์ ๊ฒฐํฉํ์ฌ ๋ ๋ณต์กํ ์ค์ ์๋๋ฆฌ์ค์๋ ๋์ํ ์ ์์ต๋๋ค.
Go ๋ฉด์ ์ค๋น๊ฐ ๋์ จ๋์?
์ธํฐ๋ํฐ๋ธ ์๋ฎฌ๋ ์ดํฐ, flashcards, ๊ธฐ์ ํ ์คํธ๋ก ์ฐ์ตํ์ธ์.
Observer ํจํด: ์ฑ๋๊ณผ ๊ณ ๋ฃจํด์ ํ์ฉํ ๊ตฌํ
Observer ํจํด์ ์ํ๊ฐ ๋ณ๊ฒฝ๋ ๋ ์ฌ๋ฌ ๊ตฌ๋ ์์๊ฒ ์๋ฆผ์ ๋ณด๋ ๋๋ค. Java๋ C#์์๋ ์ด๋ฒคํธ ๋ฆฌ์ค๋์ ์ฝ๋ฐฑ ๋ฑ๋ก์ด ํ์ํ์ง๋ง, Go๋ ๋ ๊ด์ฉ์ ์ธ ๋ฐฉ๋ฒ์ ์ ๊ณตํฉ๋๋ค. ๋ฐ๋ก ์ฑ๋์ ๋๋ค. ๊ฐ ๊ตฌ๋ ์๋ ์์ ๋ง์ ์ฑ๋์ ํตํด ์ด๋ฒคํธ๋ฅผ ์์ ํ๊ณ , ๊ณ ๋ฃจํด์ด ๋์์ ์ด๋ฒคํธ๋ฅผ ์ ๋ฌํฉ๋๋ค.
์ด ์ ๊ทผ ๋ฐฉ์์ Go์ ๋์์ฑ ํ๋ฆฌ๋ฏธํฐ๋ธ๋ฅผ ์ง์ ํ์ฉํ์ฌ ์ฝ๋ฐฑ ์คํ๊ฒํฐ๋ฅผ ๋ฐฉ์งํฉ๋๋ค.
package events
import "sync"
// Event carries a topic and payload.
type Event struct {
Topic string
Payload any
}
// Bus manages subscriptions and event dispatch.
type Bus struct {
mu sync.RWMutex
subscribers map[string][]chan Event
}
// NewBus creates an event bus.
func NewBus() *Bus {
return &Bus{
subscribers: make(map[string][]chan Event),
}
}
// Subscribe returns a channel that receives events for a topic.
func (b *Bus) Subscribe(topic string) <-chan Event {
ch := make(chan Event, 16) // buffered to avoid blocking publisher
b.mu.Lock()
b.subscribers[topic] = append(b.subscribers[topic], ch)
b.mu.Unlock()
return ch
}
// Publish sends an event to all subscribers of the topic.
func (b *Bus) Publish(topic string, payload any) {
b.mu.RLock()
defer b.mu.RUnlock()
for _, ch := range b.subscribers[topic] {
select {
case ch <- Event{Topic: topic, Payload: payload}:
default:
// subscriber too slow, drop event
}
}
}select๋ฌธ์ default ์ผ์ด์ค๋ฅผ ์ฌ์ฉํ์ฌ ๋๋ฆฐ ๊ตฌ๋
์๊ฐ ์ ์ฒด ๋ฒ์ค๋ฅผ ์ฐจ๋จํ๋ ๊ฒ์ ๋ฐฉ์งํฉ๋๋ค. ํ๋ก๋์
์์คํ
์์๋ context ๊ธฐ๋ฐ ์ทจ์ ๋ฉ์ปค๋์ฆ๊ณผ Unsubscribe ๋ฉ์๋๋ฅผ ์ถ๊ฐํ์ฌ ๊ตฌํ์ ์์ฑํด์ผ ํฉ๋๋ค.
Middleware ํจํด: HTTP ์์ฒญ ํ์ดํ๋ผ์ธ
๋ฏธ๋ค์จ์ด ์ฒด์ธ์ Go HTTP ์๋ฒ์ ํต์ฌ์
๋๋ค. ์ด ํจํด์ http.Handler๋ฅผ ๋ํํ์ฌ ๋ก๊น
, ์ธ์ฆ, ์๋ ์ ํ ๋ฑ์ ์ถ๊ฐ ๋์์ ํธ๋ค๋ฌ ์์ฒด๋ฅผ ์์ ํ์ง ์๊ณ ๋ถ์ฌํฉ๋๋ค. ํ์ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ http.Handler ์ธํฐํ์ด์ค ๋๋ถ์ ์ด๋ฌํ ๊ตฌ์ฑ์ด ๋งค์ฐ ๊ฐํธํฉ๋๋ค.
package middleware
import (
"log/slog"
"net/http"
"time"
)
// Logging records request duration and status.
func Logging(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
wrapped := &statusWriter{ResponseWriter: w, status: 200}
next.ServeHTTP(wrapped, r)
slog.Info("request",
"method", r.Method,
"path", r.URL.Path,
"status", wrapped.status,
"duration", time.Since(start),
)
})
}
// statusWriter captures the HTTP status code.
type statusWriter struct {
http.ResponseWriter
status int
}
func (w *statusWriter) WriteHeader(code int) {
w.status = code
w.ResponseWriter.WriteHeader(code)
}
// Chain applies middleware in order: first listed = outermost.
func Chain(h http.Handler, mw ...func(http.Handler) http.Handler) http.Handler {
for i := len(mw) - 1; i >= 0; i-- {
h = mw[i](h)
}
return h
}Chain ํจ์๋ ๋ฏธ๋ค์จ์ด๋ฅผ ์ ์ธ ์์๋๋ก ์ ์ฉํฉ๋๋ค. ๊ฐ ๋ฏธ๋ค์จ์ด๊ฐ ๋ค์ ํธ๋ค๋ฌ๋ฅผ ๋ํํ์ฌ ํ์ดํ๋ผ์ธ์ ํ์ฑํฉ๋๋ค. ์ด ํจํด์ chi ๊ฐ์ ์ธ๊ธฐ ๋ผ์ฐํฐ์์๋ ๋์ผํ๋ฉฐ, func(http.Handler) http.Handler ์๊ทธ๋์ฒ์ ๋ฏธ๋ค์จ์ด๋ฅผ ์์ฉํฉ๋๋ค.
๊ตฌ์กฐ์ฒด ์๋ฒ ๋ฉ์ ํตํ ์ปดํฌ์ง์
Go๋ ์์์ ์ง์ํ์ง ์์ต๋๋ค. ๋์ ๊ตฌ์กฐ์ฒด ์๋ฒ ๋ฉ(struct embedding)์ ์ฌ์ฉํ์ฌ ๋ด๋ถ ํ์ ์ ๋ฉ์๋๋ฅผ ์ธ๋ถ ํ์ ์ผ๋ก ์น๊ฒฉ์ํค๊ณ , ๊ฐํ ๊ฒฐํฉ ์์ด ์ฝ๋๋ฅผ ์ฌ์ฌ์ฉํฉ๋๋ค. ์ด๊ฒ์ ์์์ด ์๋๋๋ค. ์๋ฒ ๋ฉ๋ ํ์ ์ ์๋ฒ ๋ฉํ๋ ํ์ ์ ๋ํด ์๋ฌด๊ฒ๋ ์์ง ๋ชปํ๋ฉฐ, ๊ฐ์ ๋์คํจ์น๋ ๋ฐ์ํ์ง ์์ต๋๋ค.
์ด ๊ตฌ๋ถ์ ์ดํดํ๋ ๊ฒ์ Go ๋ฉด์ ์์ ๋งค์ฐ ์ค์ํฉ๋๋ค. ์๋ฒ ๋ฉ๊ณผ ์์์ ํผ๋ํ๋ ํ๋ณด์๋ Go์ ํ์ ์์คํ ์ ๋ํ ์ดํด๊ฐ ํผ์์ ์ด๋ผ๋ ํ๊ฐ๋ฅผ ๋ฐ๊ฒ ๋ฉ๋๋ค.
package models
import "time"
// Timestamps provides common audit fields.
type Timestamps struct {
CreatedAt time.Time
UpdatedAt time.Time
}
// User embeds Timestamps to gain CreatedAt/UpdatedAt.
type User struct {
Timestamps
ID int
Email string
}
// Order also embeds Timestamps.
type Order struct {
Timestamps
ID int
UserID int
Total float64
}User์ Order ๋ชจ๋ CreatedAt๊ณผ UpdatedAt ํ๋์ ์ง์ ์ ๊ทผํ ์ ์์ต๋๋ค. Timestamps์ ์ ์๋ ๋ฉ์๋๋ ๋ง์ฐฌ๊ฐ์ง๋ก ์น๊ฒฉ๋ฉ๋๋ค. ํต์ฌ ๊ท์น์ ์๋ฒ ๋ฉ์ด ์์์ ์ ๊ณตํ๋ ๊ฒ์ด์ง ๋์ฒด๊ฐ ์๋๋ผ๋ ์ ์
๋๋ค. User๋ Timestamps๊ฐ ์๋๋ผ Timestamps๋ฅผ ๊ฐ์ง๊ณ ์๋ ๊ฒ์
๋๋ค.
Go ๋์์ธ ํจํด ๋ฉด์ ์ง๋ฌธ
Go ๋ฉด์ ์์ ๋์์ธ ํจํด ์ง๋ฌธ์ ํ๋ณด์๊ฐ ํด๋์ ํจํด์ Go์ ํ์ ์์คํ ์ ์ ์ฉํ ์ ์๋์ง ํ์ธํฉ๋๋ค. ๋ค์์ ๊ธฐ์ ์คํฌ๋ฆฌ๋๊ณผ ํ์ฅ ๋ฉด์ ์์ ์์ฃผ ๋ฑ์ฅํ๋ ์ง๋ฌธ๋ค์ ๋๋ค.
Q: ํฉํ ๋ฆฌ ํจ์๊ฐ ํจ๋ ๋์ ์๋ฌ๋ฅผ ๋ฐํํด์ผ ํ๋ ๊ฒฝ์ฐ๋ ์ธ์ ์ธ๊ฐ?
ํฉํ ๋ฆฌ ํจ์๋ ๊ฒ์ฆ์ด ๋ฐํ์ ์
๋ ฅ(์ฌ์ฉ์ ์ ๊ณต ์ค์ , ํ๊ฒฝ ๋ณ์, ์ธ๋ถ ๋ฐ์ดํฐ)์ ์์กดํ๋ ๊ฒฝ์ฐ ์๋ฌ๋ฅผ ๋ฐํํด์ผ ํฉ๋๋ค. ํจ๋์ ํ๋ก๊ทธ๋๋จธ ์ค๋ฅ, ์ฆ API ๊ณ์ฝ์์ ๊ธ์งํ๋ nil ํฌ์ธํฐ๋ฅผ ์ ๋ฌํ๋ ๊ฒ๊ณผ ๊ฐ์ด ๋ฒ๊ทธ๋ฅผ ๋ํ๋ด๋ ์ํฉ์๋ง ์ฌ์ฉ๋ฉ๋๋ค. ํ์ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ ์ด ๊ด๋ก๋ฅผ ๋ฐ๋ฆ
๋๋ค. os.Open์ ์๋ฌ๋ฅผ ๋ฐํํ๊ณ , regexp.MustCompile์ ์ปดํ์ผ ํ์ ์์ ํจํด์ ๊ธฐ๋ํ๊ธฐ ๋๋ฌธ์ ํจ๋ํฉ๋๋ค.
Q: Functional Options ํจํด์ API ๋ฐ์ ์ ์ด๋ป๊ฒ ๊ฐ์ ํ๋๊ฐ?
์๋ก์ด WithXxx ํจ์๋ฅผ ์ถ๊ฐํ๋ ๊ฒ์ ํ์ ํธํ์ฑ์ ๊นจ๋จ๋ฆฌ์ง ์๋ ๋ณ๊ฒฝ์
๋๋ค. ๊ธฐ์กด ํธ์ถ ์ฝ๋๋ ์์ ์์ด ๊ทธ๋๋ก ๋์ํฉ๋๋ค. ์ด๋ ์ค์ ๊ตฌ์กฐ์ฒด(ํ์ ํ๋ ์ถ๊ฐ ์ ๋ชจ๋ ํธ์ถ ์ง์ ์ด ๊นจ์ง)๋ ์์น ๋งค๊ฐ๋ณ์(์ธ์ ์์ ๋ณ๊ฒฝ์ด ๋ฒ๊ทธ๋ฅผ ์ ๋ฐ)์ ๋์กฐ๋ฉ๋๋ค.
Q: Go ์ธํฐํ์ด์ค๊ฐ Java๋ C# ์ธํฐํ์ด์ค์ ๋ค๋ฅธ ์ ์ ๋ฌด์์ธ๊ฐ?
Go ์ธํฐํ์ด์ค๋ ์๋ฌต์ ์ผ๋ก ์ถฉ์กฑ๋ฉ๋๋ค. ํ์
์ด ํ์ํ ๋ฉ์๋๋ฅผ ๊ฐ์ง๊ธฐ๋ง ํ๋ฉด ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ ๊ฒ์ด๋ฉฐ, implements ์ ์ธ์ด ํ์ํ์ง ์์ต๋๋ค. ์ด๋ฅผ ํตํด "์ธํฐํ์ด์ค๋ฅผ ๋ฐ๊ณ , ๊ตฌ์กฐ์ฒด๋ฅผ ๋ฐํํ๋ค"๋ ์์น์ด ๊ฐ๋ฅํฉ๋๋ค. ์ฆ, ๊ตฌํ ์ธก์ด ์๋ ํธ์ถ ์ธก์์ ์ข์ ์ธํฐํ์ด์ค๋ฅผ ์ ์ํฉ๋๋ค. ๊ทธ ๊ฒฐ๊ณผ ๋ฎ์ ๊ฒฐํฉ๋์ ๋์ ํ
์คํธ ์ฉ์ด์ฑ์ด ๋ฌ์ฑ๋ฉ๋๋ค.
๋ฉด์ ๊ด์ ํ๋ณด์์๊ฒ ๊ตฌ์ฒด์ ์ธ ์์กด์ฑ์ ์ธํฐํ์ด์ค๋ก ๋ฆฌํฉํ ๋งํ๋๋ก ์์ฃผ ์์ฒญํฉ๋๋ค. ํ ์คํธ ํฌ์ธํธ๋ ํ์ํ ์ต์ํ์ ๋ฉ์๋ ์ธํธ๋ฅผ ์๋ณํ๊ณ , ๊ตฌํ ์ธก์ด ์๋ ์๋น์ ์ธก์์ ์ธํฐํ์ด์ค๋ฅผ ์ถ์ถํ ์ ์๋์ง ์ฌ๋ถ์ ๋๋ค.
Q: ์ฑ๋ ๊ธฐ๋ฐ Observer์ ์ฝ๋ฐฑ ๊ธฐ๋ฐ Observer์ ์ฐจ์ด์ ์ ๋ฌด์์ธ๊ฐ?
์ฑ๋์ ๋ฐํ์์ ๊ตฌ๋
์๋ฅผ ์๊ฐ๊ณผ ๊ณต๊ฐ ๋ชจ๋์์ ๋ถ๋ฆฌํฉ๋๋ค. ๋ฐํ์๋ ๊ตฌ๋
์ ํจ์์ ๋ํ ์ฐธ์กฐ๋ฅผ ๋ณด์ ํ์ง ์๊ณ ์ฑ๋์ ์ ์ก๋ง ํฉ๋๋ค. ๊ตฌ๋
์๋ ๋ฒํผ๋ง๋ ์ฑ๋์ ์ฌ์ฉํ์ฌ ์์ ์ ์๋๋ก ์ด๋ฒคํธ๋ฅผ ์ฒ๋ฆฌํ ์ ์์ต๋๋ค. select๋ฌธ์ ํตํด ํ์์์ ์ฒ๋ฆฌ, context๋ฅผ ํตํ ์ทจ์, ์ฌ๋ฌ ์ด๋ฒคํธ ์์ค์ ๋ฉํฐํ๋ ์ฑ์ด ๊ฐ๋ฅํฉ๋๋ค. ์ด๋ฌํ ๊ธฐ๋ฅ์ ์ฝ๋ฐฑ์ผ๋ก๋ ์ฝ๊ฒ ์ป์ ์ ์์ต๋๋ค.
๊ตฌ๋
์ ์ฑ๋์ ๋ซ๋ ๊ฒ์ ์์ผ๋ฉด ๊ณ ๋ฃจํด ๋์๊ฐ ๋ฐ์ํฉ๋๋ค. ๋ชจ๋ Subscribe์๋ ์ฑ๋์ ๋ซ๊ณ ๋ฒ์ค์์ ์ ๊ฑฐํ๋ ๋์ํ๋ Unsubscribe๊ฐ ์์ด์ผ ํฉ๋๋ค.
Go ๋ฉด์ ์ค๋น๊ฐ ๋์ จ๋์?
์ธํฐ๋ํฐ๋ธ ์๋ฎฌ๋ ์ดํฐ, flashcards, ๊ธฐ์ ํ ์คํธ๋ก ์ฐ์ตํ์ธ์.
๊ฒฐ๋ก
- Functional Options ํจํด์ Builder์ ์ค์ ๊ตฌ์กฐ์ฒด๋ฅผ ๊ธฐ์กด ํธ์ถ ์ฝ๋๋ฅผ ๊นจ๋จ๋ฆฌ์ง ์๋ ๊น๋ํ๊ณ ํ์ฅ ๊ฐ๋ฅํ API๋ก ๋์ฒดํฉ๋๋ค
- Go์ Strategy ํจํด์ ์ธํฐํ์ด์ค ๊ธฐ๋ฐ ๋คํ์ฑ์ ๋์ํฉ๋๋ค. ์ธํฐํ์ด์ค ์ ์๋ ๊ตฌํ ์ธก์ด ์๋ ์๋น์ ์ธก์์ ์ํํฉ๋๋ค
- ํฉํ ๋ฆฌ ํจ์๋ ์ ๋ก ๊ฐ ์ด๊ธฐํ๋ก๋ ๋ณด์ฅํ ์ ์๋ ๋ถ๋ณ ์กฐ๊ฑด์ ๊ฐ์ ํฉ๋๋ค. ๋ฐํ์ ์ ๋ ฅ์๋ ์๋ฌ๋ฅผ ๋ฐํํ๊ณ , ํจ๋์ ํ๋ก๊ทธ๋๋จธ ๋ฒ๊ทธ์๋ง ์ฌ์ฉํฉ๋๋ค
- ์ฑ๋ ๊ธฐ๋ฐ Observer๋ ๊ณ ๋ฃจํด๊ณผ select๋ฅผ ํ์ฉํ์ฌ ์ฝ๋ฐฑ ์ฒด์ธ ์์ด ๋์์ ์ด๊ณ ๋์จํ๊ฒ ๊ฒฐํฉ๋ ์ด๋ฒคํธ ์ ๋ฌ์ ๊ตฌํํฉ๋๋ค
- HTTP ๋ฏธ๋ค์จ์ด ์ฒด์ธ์
func(http.Handler) http.Handler์๊ทธ๋์ฒ๋ฅผ ํตํด ๊ตฌ์ฑ๋๋ฉฐ, ํ์ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ์๋ํํฐ ๋ผ์ฐํฐ์์ ๋์ผํฉ๋๋ค - ๊ตฌ์กฐ์ฒด ์๋ฒ ๋ฉ์ ์์์ ์ ๊ณตํ๋ ๊ฒ์ด์ง ์์์ด ์๋๋๋ค. ์ด ๊ตฌ๋ถ์ ์ดํดํ๋ ๊ฒ์ด Go๋ฅผ ๊น์ด ์ดํดํ๋ ๊ฐ๋ฐ์์ Java ํจํด์ ๊ทธ๋๋ก ์ฎ๊ธฐ๋ ๊ฐ๋ฐ์๋ฅผ ๊ตฌ๋ถํฉ๋๋ค
- ๋ฉด์ ์์์ ์ฑ๊ณต์ GoF ์ ์์ ์๊ธฐ๊ฐ ์๋๋ผ ํจํด์ Go์ ํ์ ์์คํ ์ ๊ด์ฉ์ ์ผ๋ก ์ ์ฉํ๋ ๋ฅ๋ ฅ์ ๋ฌ๋ ค ์์ต๋๋ค
์ฐ์ต์ ์์ํ์ธ์!
๋ฉด์ ์๋ฎฌ๋ ์ดํฐ์ ๊ธฐ์ ํ ์คํธ๋ก ์ง์์ ํ ์คํธํ์ธ์.
ํ๊ทธ
๊ณต์
๊ด๋ จ ๊ธฐ์ฌ

Go 1.26 ๋ฉด์ ๋๋น: Green Tea GC, go fix ๋๊ตฌ, ์คํ ์ต์ ํ ์๋ฒฝ ์ ๋ฆฌ
Go 1.26 ๋ฉด์ ์์ ์์ฃผ ์ถ์ ๋๋ Green Tea ๊ฐ๋น์ง ์ปฌ๋ ํฐ, ์๋ก์์ง go fix ๋๊ตฌ, ์ฌ๋ผ์ด์ค ์คํ ํ ๋น ์ต์ ํ ๋ฑ ํต์ฌ ๋ณ๊ฒฝ์ฌํญ์ ์์ธํ ์ ๋ฆฌํฉ๋๋ค.

Go ๋ฉด์ ํต์ฌ 25๋ฌธํญ: ๊ฐ๋ฐ์๋ฅผ ์ํ ์์ ๊ฐ์ด๋
Go ๋ฉด์ ์ ๊ฐ์ฅ ๋ง์ด ๋ฑ์ฅํ๋ 25๊ฐ ์ง๋ฌธ์ผ๋ก ํฉ๊ฒฉ์ ๋ ธ๋ฆฌ์ธ์. ๊ณ ๋ฃจํด, ์ฑ๋, ์ธํฐํ์ด์ค, ๋์์ฑ ํจํด์ ์ฝ๋ ์์ ๋ก ์ ๋ฆฌํ์ต๋๋ค.

Go ๊ธฐ์ ๋ฉด์ : Goroutine, Channel, ๋์์ฑ ํจํด ์๋ฒฝ ๊ฐ์ด๋
Go ๊ธฐ์ ๋ฉด์ ์์ ์์ฃผ ์ถ์ ๋๋ goroutine, channel, ๋์์ฑ ๊ด๋ จ ์ง๋ฌธ์ ๋ค๋ฃน๋๋ค. ํ๋ก๋์ ์์ค์ ์ฝ๋ ์์ ์ ๊ฐ ๋ต๋ณ์ ์ค๊ณ ๊ทผ๊ฑฐ๋ฅผ 2026๋ ๋ฉด์ ๋๋น์ฉ์ผ๋ก ์์ธํ ์ค๋ช ํฉ๋๋ค.