ข้ามไปยังเนื้อหา

Static Binaries และ Go Toolchain

เมื่อคุณจะ ship แอป TypeScript/Node.js คุณต้องเตรียมสิ่งเหล่านี้:

  1. Node.js runtime (เวอร์ชันที่ถูกต้อง — จัดการด้วย .nvmrc, Docker base image ฯลฯ)
  2. ทุก npm package ที่โค้ดของคุณพึ่งพา (node_modules/ — มักมีขนาดหลายร้อย MB)
  3. ขั้นตอน transpile (tsc, esbuild, swc ฯลฯ) เพื่อแปลง TypeScript เป็น JavaScript

การ deploy หมายถึงการทำให้แน่ใจว่าทั้งสามชิ้นส่วนนี้มีครบและเวอร์ชันตรงกันบนทุกเครื่องปลายทาง

โปรแกรม Go คอมไพล์ออกมาเป็น static binary เดี่ยว ที่บรรจุโค้ดของคุณ, dependencies ทั้งหมด และ Go runtime ไว้ในตัว — ลิงก์รวมเข้าไปทั้งหมด เครื่องปลายทางไม่ต้องติดตั้งอะไรเพิ่มเลย ไม่ต้องมี Go ติดตั้ง ไม่ต้องมี package manager ไม่ต้องมี interpreter

TypeScript
# TypeScript deployment checklist
# 1. Install Node.js (exact version)
node --version # must match .nvmrc
# 2. Install dependencies
npm install # downloads node_modules/ (~hundreds of MB)
# 3. Build
npx tsc --build
# 4. Run
node dist/index.js
# Dockerfile must include:
# FROM node:20-alpine
# COPY package*.json ./
# RUN npm ci
# COPY dist/ ./dist/
# CMD ["node", "dist/index.js"]
Go
# Go deployment checklist
# 1. Build
go build -o myapp ./cmd/myapp
# 2. Copy binary to target (that's it)
scp myapp user@server:/usr/local/bin/
# 3. Run
./myapp
# Dockerfile can be:
# FROM scratch <-- empty base image!
# COPY myapp /myapp
# CMD ["/myapp"]
#
# Final image is often < 10 MB vs 200–500 MB for Node

Go มี cross-compilation มาในตัว แค่ตั้งค่า GOOS (OS ปลายทาง) และ GOARCH (CPU ปลายทาง) เป็น environment variables แล้วรัน go build ไม่ต้องมี toolchain เพิ่ม ไม่ต้อง emulate ด้วย Docker ไม่ต้องมีขั้นตอน CI แยกสำหรับแต่ละ platform

TypeScript
# TypeScript cross-platform is handled by Node.js itself
# But native addons (node-gyp, .node files) break cross-platform
# pkg / nexe / sea bundle Node + app, but are complex and large
Go
# Build for Linux amd64 on a Mac
GOOS=linux GOARCH=amd64 go build -o myapp-linux ./cmd/myapp
# Build for Windows
GOOS=windows GOARCH=amd64 go build -o myapp.exe ./cmd/myapp
# Build for ARM (Raspberry Pi, AWS Graviton, Apple Silicon)
GOOS=linux GOARCH=arm64 go build -o myapp-arm64 ./cmd/myapp
# All three commands run on your laptopno emulation needed
# Each output is a standalone executable for that platform

Go ออกแบบมาโดยตั้งความเร็วในการ compile เป็นเป้าหมายระดับ first-class โค้ดเบสขนาดใหญ่ที่อาจใช้เวลาหลายนาทีใน C++ หรือแม้แต่หลายวินาทีใน TypeScript (รัน tsc แบบเต็ม) จะ compile เสร็จในไม่กี่วินาทีหรือต่ำกว่าวินาทีใน Go เพราะ standard library ถูก pre-compile ไว้แล้ว, dependency graph ชัดเจน และไม่มี overhead จากการตรวจจับ circular import

TypeScript
# TypeScript build times (realistic examples)
# tsc --build on a medium NestJS project: 5–30s
# esbuild (fastest TS bundler): 0.5–3s
# But: tsc is also used for type-checking only (no emit)
# Type-check of a large monorepo: 30s–3min
Go
# Go build times
# go build ./... on a medium service: < 2s
# go test ./... (build + run tests): 210s
# go vet ./... (static analysis): < 1s
# The toolchain is one binaryno plugin ecosystem needed:
go build # compile
go test # test
go vet # static analysis
go fmt # format (gofmtno config needed)
go mod # dependency management
Go binary มีอะไรที่แอป TypeScript ที่คอมไพล์แล้วไม่มี?
คุณจะ cross-compile โปรแกรม Go สำหรับ Linux จากเครื่อง Mac ได้อย่างไร?
env var ใดที่ปิด cgo เพื่อให้ได้ binary แบบ static อย่างสมบูรณ์?
go.sum ให้สิ่งใดที่ package-lock.json ไม่มี?