gRPC vs REST

When to Use Which in Go

Web
Use gRPC for high-performance internal microservices with strict contracts and REST for public APIs requiring broad compatibility and simplicity.

Use gRPC for high-performance, internal microservices requiring strong typing and streaming; use REST for public APIs needing broad compatibility and simplicity. In Go, choose gRPC when leveraging Protocol Buffers for strict contracts and bidirectional streams, and REST when using net/http for standard JSON endpoints.

// gRPC: Define .proto, generate code, use grpc.NewServer()
// REST: Use net/http.HandleFunc() with JSON marshaling

import (
    "net/http"
    "encoding/json"
)

// REST Example
func handler(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "application/json")
    json.NewEncoder(w).Encode(map[string]string{"status": "ok"})
}

func main() {
    http.HandleFunc("/api", handler)
    http.ListenAndServe(":8080", nil)
}