Skip to content
← All Skills
🔵

Go

ภาษาหลักสำหรับ backend microservices — trading engine, API gateway, real-time data processing

What I Can Do

  • สร้าง RESTful API ด้วย Gin / Fiber framework
  • ออกแบบ microservices architecture สำหรับ trading platform
  • เขียน concurrent data pipeline ด้วย goroutine + channel
  • Integrate กับ Kafka, PostgreSQL, Redis, MongoDB
  • เขียน unit test และ integration test อย่างครบถ้วน

Commands I Use Daily

bash
# run โปรเจค (ใช้ตอน dev)
go run ./cmd/server

# build binary สำหรับ production
CGO_ENABLED=0 go build -o main ./cmd/server

# run ทุก test ใน project
go test ./...

# run test แบบ verbose + race detection
go test -v -race ./...

# จัดการ dependencies
go mod tidy        # ลบ unused, เพิ่ม missing
go mod download    # download ล่วงหน้า (ใช้ใน Dockerfile)

# code quality
go vet ./...       # ตรวจ common mistakes
go fmt ./...       # format code

# generate code (mock, protobuf, etc.)
go generate ./...

# profiling — หา memory leak, CPU bottleneck
go tool pprof http://localhost:6060/debug/pprof/heap
go tool pprof http://localhost:6060/debug/pprof/profile?seconds=30

Go Basics (Types, Structs, Interfaces)

  • Basic types: string, int, float64, bool
  • Struct คือ collection ของ fields — ใช้แทน class
  • Interface คือ set ของ method signatures — type ใดก็ตามที่ implement methods ครบถือว่า satisfy interface โดยไม่ต้อง declare (implicit implementation)

Functions

  • ฟังก์ชันเป็น first-class citizen — ส่งเป็น argument, return จากฟังก์ชันอื่นได้
  • รองรับ multiple return values — pattern ที่ใช้บ่อยคือ (result, error)
  • Named return values, variadic functions (...)

Error Handling (if err != nil)

Go ไม่มี exceptions — ใช้ return error เป็น value แล้วตรวจด้วย if err != nil ทุกครั้ง error เป็น interface ที่มี method Error() string เดียว ทำให้สร้าง custom error types ได้ง่าย

Packages & Modules

  • Package = directory ที่รวม .go files ที่เกี่ยวข้อง
  • Module = collection ของ packages, manage ด้วย go.mod
  • Exported names ขึ้นต้นด้วยตัวใหญ่ (MyFunc), unexported ขึ้นต้นตัวเล็ก (myFunc)

Slices & Maps

  • Slice — dynamic array, ใช้ append() เพิ่ม element, len() / cap() ดูขนาด
  • Map — key-value store, make(map[string]int), check existence ด้วย val, ok := m[key]

For Loop

Go มี loop แบบเดียวคือ for — ใช้แทน while, for-each ด้วย range สำหรับ iterate slice, map, channel, string

Pointer Basics

  • &x = address ของ x, *p = value ที่ pointer p ชี้อยู่
  • ใช้ pointer เมื่อต้องการ mutate ค่าใน function หรือหลีกเลี่ยงการ copy struct ใหญ่
  • Go ไม่มี pointer arithmetic

Goroutines & Channels

  • Goroutine — lightweight thread, สร้างด้วย go func() ใช้ memory แค่ ~2KB
  • Channel — ใช้สื่อสารระหว่าง goroutines, ch := make(chan int), ส่งด้วย ch <- val, รับด้วย val := <-ch
  • Buffered vs unbuffered channels, select สำหรับ multiplex หลาย channels

Context Package

context.Context ใช้สำหรับ cancellation, timeout, และส่ง request-scoped values ข้าม function boundaries — ต้องส่งเป็น parameter แรกของทุกฟังก์ชันที่เกี่ยวกับ I/O

  • context.WithTimeout — auto cancel หลัง duration ที่กำหนด
  • context.WithCancel — manual cancel
  • ตรวจ ctx.Err() หรือ <-ctx.Done() เพื่อ handle cancellation

Interface Design

  • Keep interfaces small — 1-2 methods (เช่น io.Reader, io.Writer)
  • Define interfaces ที่ consumer ไม่ใช่ provider — "accept interfaces, return structs"
  • ใช้ interface สำหรับ dependency injection และ testability

Struct Embedding

Go ใช้ composition แทน inheritance — embed struct เข้าไปใน struct อื่นเพื่อ "inherit" methods และ fields โดยตรง

Testing

  • go test + *_test.go files — test function ชื่อ TestXxx(t *testing.T)
  • Table-driven tests เป็น idiomatic pattern
  • t.Run() สำหรับ subtests, t.Parallel() สำหรับ parallel tests
  • Benchmark ด้วย BenchmarkXxx(b *testing.B)

Dependency Injection

ส่ง dependencies เข้าผ่าน constructor function แทนการ import ตรง — ทำให้ test ง่ายเพราะ mock ได้

JSON Marshaling

ใช้ struct tags (json:"field_name") กำหนด JSON field names — json.Marshal / json.Unmarshal สำหรับ encode/decode, omitempty สำหรับ skip zero values

HTTP Server

net/http package + framework (Gin, Fiber) สำหรับสร้าง HTTP server — middleware pattern, routing, request/response handling

Concurrency Patterns

  • errgroup — run หลาย goroutines แล้วรอทุกตัวจบ, cancel ถ้าตัวใดตัวหนึ่ง error
  • Worker pool — จำกัดจำนวน goroutines ด้วย buffered channel เป็น semaphore
  • Fan-out/Fan-in — กระจายงานไปหลาย goroutines (fan-out) แล้วรวม results กลับ (fan-in)

Profiling (pprof)

ใช้ net/http/pprof expose profiling endpoints — CPU profile, memory (heap) profile, goroutine dump เพื่อหา bottleneck และ memory leak

Reflection

reflect package ใช้ inspect types และ values ตอน runtime — ใช้ใน generic libraries เช่น JSON encoder, ORM แต่ควรหลีกเลี่ยงใน application code เพราะช้าและ type-unsafe

Unsafe

unsafe package ใช้ bypass Go type system — pointer conversion, struct memory layout ใช้ตอนต้อง interop กับ C หรือ optimize critical paths เท่านั้น

Build Tags

ใช้ //go:build directive เพื่อ include/exclude files ตาม conditions เช่น OS, architecture, custom tags — ใช้แยก integration tests, platform-specific code

Code Generation

go generate + tools เช่น mockgen, protoc, stringer — generate boilerplate code จาก annotations, ลด manual work และ ensure consistency

Graceful Shutdown

จัดการ OS signals (SIGTERM, SIGINT) เพื่อ shutdown server อย่าง graceful — drain existing connections, flush buffers, close database connections ก่อน exit

Related Skills

  • Docker — containerize Go services
  • Kafka — event-driven communication ระหว่าง services
  • PostgreSQL — primary data store สำหรับ Go services