Go (Goroutines)
Why Go?
Section titled “Why Go?”- ✅ Compiled to single binary
- ✅ No runtime dependencies
- ✅ Built-in concurrency (goroutines)
- ✅ Fast startup time
- ✅ Low memory footprint
Simple HTTP Server
Section titled “Simple HTTP Server”package main
import ( "fmt" "net/http")
func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello from Go!") })
fmt.Println("Server running on :8000") http.ListenAndServe(":8000", nil)}Build & Deploy
Section titled “Build & Deploy”# Build binarygo build -o server main.go
# Build for Linux (cross-compile)GOOS=linux GOARCH=amd64 go build -o server main.go
# Run./serverSystemd Service
Section titled “Systemd Service”/etc/systemd/system/snapcode.service:
[Unit]Description=SnapCode Go ServerAfter=network.target
[Service]Type=simpleUser=www-dataWorkingDirectory=/var/www/snapcodeExecStart=/var/www/snapcode/serverRestart=alwaysRestartSec=5
[Install]WantedBy=multi-user.targetsudo systemctl daemon-reloadsudo systemctl enable snapcodesudo systemctl start snapcodeGoroutines
Section titled “Goroutines”// Concurrent request handling (built-in)http.ListenAndServe(":8000", nil)// Each request runs in its own goroutine automatically!
// Manual goroutinego func() { // Background task}()vs Other Runtimes
Section titled “vs Other Runtimes”| Aspect | Go | Python | Node.js | Rust |
|---|---|---|---|---|
| Deployment | Binary | Interpreter | Runtime | Binary |
| Dependencies | None | pip | npm | None |
| Startup | ~10ms | ~100ms | ~50ms | ~5ms |
| Memory | Low | Medium | Medium | Lowest |
| Concurrency | Goroutines | Async/Threads | Event loop | Async/Threads |
No Process Manager Needed!
Section titled “No Process Manager Needed!”Go binaries are self-contained. Just use systemd:
- ✅ Auto-restart on crash
- ✅ Log management (journald)
- ✅ No Supervisor/PM2 needed