Un serveur web sans framework
La bibliothèque standard de Go inclut tout ce qu'il faut pour créer un serveur web. Pas besoin de framework externe :
Un handler HTTP en Go a la signature func(w http.ResponseWriter, r *http.Request) et ne renvoie aucune valeur. Avant de dérouler : comment envoie-t-on alors la réponse au client, et à quoi servent précisément w et r ?
Voir la réponse
Un handler Go ne renvoie rien (pas de valeur de retour). On envoie la réponse en écrivant dans w, le http.ResponseWriter : par exemple fmt.Fprintf(w, "Bonjour") ou w.Write([]byte("...")). w est le flux de sortie vers le client : on y écrit le corps, on règle le code de statut avec w.WriteHeader(...) et les en-têtes avec w.Header(). r, le *http.Request, contient tout ce qui vient du client : la méthode, l'URL, les paramètres (r.URL.Query()), les en-têtes, le corps. Modèle mental : on lit la requête dans r, on écrit la réponse dans w. Si on n'écrit rien dans w, le client reçoit une réponse 200 OK vide.
package main
import (
"fmt"
"net/http"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Bonjour depuis Go !")
})
http.ListenAndServe(":8080", nil)
}
Lancez avec go run main.go et ouvrez http://localhost:8080. Votre serveur est en ligne.
Python a bien un module http.server et Node un module http, mais pour un vrai service on passe en général par un framework : Flask ou Django, Express. En Go, la bibliothèque standard suffit. Et ce serveur gère chaque requête dans sa propre goroutine : concurrence gratuite.
net/http la route vers le bon handler (dans sa propre goroutine), qui écrit la réponse dans w.En production, on configure le serveur. http.HandleFunc + ListenAndServe(":8080", nil) reposent sur un routeur global, pratique pour apprendre. En réel, on crée un http.ServeMux explicite et un http.Server avec des timeouts (sinon une connexion lente peut monopoliser une ressource indéfiniment) :
mux := http.NewServeMux()
mux.HandleFunc("/api/tasks", handleTasks)
srv := &http.Server{
Addr: ":8080",
Handler: mux,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}
srv.ListenAndServe()
Plusieurs routes
Ajoutez autant de routes que nécessaire avec http.HandleFunc :
func main() {
http.HandleFunc("/", handleHome)
http.HandleFunc("/api/health", handleHealth)
http.HandleFunc("/api/users", handleUsers)
fmt.Println("Serveur démarré sur :8080")
http.ListenAndServe(":8080", nil)
}
func handleHome(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Page d'accueil")
}
func handleHealth(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, `{"status": "ok"}`)
}
func handleUsers(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, `[{"name": "Alice"}, {"name": "Bob"}]`)
}
Chaque handler reçoit deux paramètres :
w http.ResponseWriter: pour écrire la réponser *http.Request: la requête entrante (méthode, URL, body, headers)
JSON : encoding/json
Le package encoding/json convertit les structs Go en JSON et inversement :
type User struct {
Name string `json:"name"`
Email string `json:"email"`
Age int `json:"age"`
}
// Struct → JSON (NewEncoder écrit directement dans w)
func handleGetUser(w http.ResponseWriter, r *http.Request) {
user := User{Name: "Alice", Email: "alice@example.com", Age: 30}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(user)
}
// JSON → Struct (NewDecoder lit le corps de la requête)
func handleCreateUser(w http.ResponseWriter, r *http.Request) {
var user User
err := json.NewDecoder(r.Body).Decode(&user)
if err != nil {
http.Error(w, "JSON invalide", http.StatusBadRequest)
return
}
fmt.Fprintf(w, "Utilisateur créé : %s", user.Name)
}
Les tags `json:"name"` après chaque champ contrôlent le nom dans le JSON. Sans eux, Go utiliserait Name avec une majuscule. Pour convertir depuis ou vers un []byte plutôt qu'un flux, on utilise json.Marshal et json.Unmarshal ; l'exercice plus bas utilise json.Marshal.
Une API REST complète
Combinons tout pour créer une API de gestion de tâches :
type Task struct {
ID int `json:"id"`
Title string `json:"title"`
Done bool `json:"done"`
}
var tasks = []Task{
{ID: 1, Title: "Apprendre Go", Done: false},
{ID: 2, Title: "Créer une API", Done: false},
}
func handleTasks(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
switch r.Method {
case "GET":
json.NewEncoder(w).Encode(tasks)
case "POST":
var task Task
if err := json.NewDecoder(r.Body).Decode(&task); err != nil {
http.Error(w, "JSON invalide", http.StatusBadRequest)
return
}
task.ID = len(tasks) + 1
tasks = append(tasks, task)
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(task)
default:
http.Error(w, "Méthode non autorisée", http.StatusMethodNotAllowed)
}
}
Pour rester simple, cet exemple ne protège pas la variable tasks contre les accès concurrents. Or net/http traite chaque requête dans sa propre goroutine : deux POST simultanés provoqueraient une data race sur le slice. En production, on protégerait tasks avec un sync.Mutex (voir la leçon Goroutines et channels).
Cette API gère GET /api/tasks pour lister et POST /api/tasks pour créer. Testez-la avec curl :
# Lister les tâches
curl http://localhost:8080/api/tasks
# Créer une tâche
curl -X POST -d '{"title":"Première tâche"}' http://localhost:8080/api/tasks
Le serveur HTTP ne tourne pas ici, mais l'encodage JSON oui. À vous de modifier le programme : ajoutez à User un champ Admin bool avec le tag `json:"admin"`, donnez-lui la valeur true dans main, et vérifiez que le JSON affiché contient "admin":true :
package main
import (
"encoding/json"
"fmt"
)
type User struct {
Name string `json:"name"`
Email string `json:"email"`
Age int `json:"age"`
}
func main() {
user := User{Name: "Alice", Email: "alice@example.com", Age: 30}
data, _ := json.Marshal(user)
fmt.Println(string(data))
}
Tester un handler sans serveur : httptest
Le serveur lui-même ne tourne pas dans le bac à sable, mais un handler n'est qu'une fonction : on peut l'appeler directement, sans port ni réseau. Le paquet net/http/httptest fournit les deux pièces qui manquent :
httptest.NewRequest(méthode, url, corps)fabrique une requête*http.Request.httptest.NewRecorder()fabrique un fauxhttp.ResponseWriterqui enregistre la réponse : on lit ensuiterec.Code(le statut),rec.Body(le corps) etrec.Header().
Un test Go est une fonction TestXxx(t *testing.T) lancée par go test, et t.Errorf signale un échec : la leçon 11 les détaille. Ici, il suffit de les lire.
À vous : ci-dessous, une version réduite du POST de l'API de tâches, avec ses deux tests. Exécutez : le premier test échoue (rouge) car le client reçoit 200 au lieu de 201. Corrigez createTask (le bug est dans le handler, pas dans le test), puis exécutez à nouveau : ok.
package main
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
type Task struct {
ID int `json:"id"`
Title string `json:"title"`
}
func createTask(w http.ResponseWriter, r *http.Request) {
var task Task
if err := json.NewDecoder(r.Body).Decode(&task); err != nil {
http.Error(w, "JSON invalide", http.StatusBadRequest)
return
}
task.ID = 1
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(task)
w.WriteHeader(http.StatusCreated)
}
func TestCreateTask(t *testing.T) {
body := strings.NewReader(`{"title":"Apprendre Go"}`)
req := httptest.NewRequest(http.MethodPost, "/api/tasks", body)
rec := httptest.NewRecorder()
createTask(rec, req)
if rec.Code != http.StatusCreated {
t.Errorf("code = %d, attendu %d", rec.Code, http.StatusCreated)
}
if !strings.Contains(rec.Body.String(), `"title":"Apprendre Go"`) {
t.Errorf("corps = %q, attendu le titre de la tâche", rec.Body.String())
}
}
func TestCreateTaskInvalidJSON(t *testing.T) {
req := httptest.NewRequest(http.MethodPost, "/api/tasks", strings.NewReader("pas du json"))
rec := httptest.NewRecorder()
createTask(rec, req)
if rec.Code != http.StatusBadRequest {
t.Errorf("code = %d, attendu %d", rec.Code, http.StatusBadRequest)
}
}
Pour aller plus loin : ajoutez au premier test une vérification de l'en-tête, rec.Header().Get("Content-Type") doit valoir application/json.
Voir la solution
Écrire le corps envoie l'en-tête de la réponse avec le statut par défaut, 200 : un WriteHeader appelé après coup est ignoré. Le statut doit donc être fixé avant d'écrire :
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(task)
A web server without a framework
Go's standard library includes everything you need to create a web server. No external framework required:
An HTTP handler in Go has the signature func(w http.ResponseWriter, r *http.Request) and returns no value. Before scrolling: how do you send the response to the client then, and what exactly are w and r for?
See the answer
A Go handler returns nothing (no return value). You send the response by writing into w, the http.ResponseWriter: for example fmt.Fprintf(w, "Hello") or w.Write([]byte("...")). w is the output stream to the client: you write the body there, set the status code with w.WriteHeader(...), and headers with w.Header(). r, the *http.Request, holds everything coming from the client: the method, URL, query parameters (r.URL.Query()), headers, and body. Mental model: you read the request from r, you write the response into w. If you write nothing into w, the client receives an empty 200 OK response.
package main
import (
"fmt"
"net/http"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello from Go!")
})
http.ListenAndServe(":8080", nil)
}
Run with go run main.go and open http://localhost:8080. Your server is live.
Python does have an http.server module and Node an http module, but for a real service people usually reach for a framework: Flask or Django, Express. In Go, the standard library is enough. And this server handles each request in its own goroutine — free concurrency.
net/http routes it to the right handler (in its own goroutine), which writes the response into w.In production, you configure the server. http.HandleFunc + ListenAndServe(":8080", nil) rely on a global router, handy for learning. In the real world you create an explicit http.ServeMux and an http.Server with timeouts (otherwise a slow connection can tie up a resource indefinitely):
mux := http.NewServeMux()
mux.HandleFunc("/api/tasks", handleTasks)
srv := &http.Server{
Addr: ":8080",
Handler: mux,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}
srv.ListenAndServe()
Multiple routes
Add as many routes as needed with http.HandleFunc:
func main() {
http.HandleFunc("/", handleHome)
http.HandleFunc("/api/health", handleHealth)
http.HandleFunc("/api/users", handleUsers)
fmt.Println("Server started on :8080")
http.ListenAndServe(":8080", nil)
}
func handleHome(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Home page")
}
func handleHealth(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, `{"status": "ok"}`)
}
func handleUsers(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, `[{"name": "Alice"}, {"name": "Bob"}]`)
}
Each handler receives two parameters:
w http.ResponseWriter— to write the responser *http.Request— the incoming request (method, URL, body, headers)
JSON: encoding/json
The encoding/json package converts Go structs to JSON and vice versa:
type User struct {
Name string `json:"name"`
Email string `json:"email"`
Age int `json:"age"`
}
// Struct → JSON (NewEncoder writes straight to w)
func handleGetUser(w http.ResponseWriter, r *http.Request) {
user := User{Name: "Alice", Email: "alice@example.com", Age: 30}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(user)
}
// JSON → Struct (NewDecoder reads the request body)
func handleCreateUser(w http.ResponseWriter, r *http.Request) {
var user User
err := json.NewDecoder(r.Body).Decode(&user)
if err != nil {
http.Error(w, "Invalid JSON", http.StatusBadRequest)
return
}
fmt.Fprintf(w, "User created: %s", user.Name)
}
The tags `json:"name"` after each field control the name in JSON. Without them, Go would use Name with a capital letter. To convert from or to a []byte rather than a stream, use json.Marshal and json.Unmarshal; the exercise below uses json.Marshal.
A complete REST API
Let's combine everything to create a task management API:
type Task struct {
ID int `json:"id"`
Title string `json:"title"`
Done bool `json:"done"`
}
var tasks = []Task{
{ID: 1, Title: "Learn Go", Done: false},
{ID: 2, Title: "Create an API", Done: false},
}
func handleTasks(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
switch r.Method {
case "GET":
json.NewEncoder(w).Encode(tasks)
case "POST":
var task Task
if err := json.NewDecoder(r.Body).Decode(&task); err != nil {
http.Error(w, "Invalid JSON", http.StatusBadRequest)
return
}
task.ID = len(tasks) + 1
tasks = append(tasks, task)
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(task)
default:
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
}
To keep it simple, this example doesn't protect the tasks variable against concurrent access. But net/http serves each request in its own goroutine: two simultaneous POSTs would cause a data race on the slice. In production you'd guard tasks with a sync.Mutex (see the Goroutines and channels lesson).
This API handles GET /api/tasks to list and POST /api/tasks to create. Test it with curl:
# List tasks
curl http://localhost:8080/api/tasks
# Create a task
curl -X POST -d '{"title":"Test the API"}' http://localhost:8080/api/tasks
The HTTP server doesn't run here, but JSON encoding does. Your turn to modify the program: add an Admin bool field with the tag `json:"admin"` to User, set it to true in main, and check that the printed JSON contains "admin":true:
package main
import (
"encoding/json"
"fmt"
)
type User struct {
Name string `json:"name"`
Email string `json:"email"`
Age int `json:"age"`
}
func main() {
user := User{Name: "Alice", Email: "alice@example.com", Age: 30}
data, _ := json.Marshal(user)
fmt.Println(string(data))
}
Testing a handler without a server: httptest
The server itself does not run in the sandbox, but a handler is just a function: you can call it directly, with no port and no network. The net/http/httptest package provides the two missing pieces:
httptest.NewRequest(method, url, body)builds an*http.Request.httptest.NewRecorder()builds a fakehttp.ResponseWriterthat records the response: you then readrec.Code(the status),rec.Body(the body) andrec.Header().
A Go test is a TestXxx(t *testing.T) function run by go test, and t.Errorf reports a failure: lesson 11 covers them in detail. Here, you only need to read them.
Your turn: below is a reduced version of the task API's POST, with its two tests. Run it: the first test fails (red) because the client gets 200 instead of 201. Fix createTask (the bug is in the handler, not in the test), then run again: ok.
package main
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
type Task struct {
ID int `json:"id"`
Title string `json:"title"`
}
func createTask(w http.ResponseWriter, r *http.Request) {
var task Task
if err := json.NewDecoder(r.Body).Decode(&task); err != nil {
http.Error(w, "Invalid JSON", http.StatusBadRequest)
return
}
task.ID = 1
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(task)
w.WriteHeader(http.StatusCreated)
}
func TestCreateTask(t *testing.T) {
body := strings.NewReader(`{"title":"Learn Go"}`)
req := httptest.NewRequest(http.MethodPost, "/api/tasks", body)
rec := httptest.NewRecorder()
createTask(rec, req)
if rec.Code != http.StatusCreated {
t.Errorf("code = %d, want %d", rec.Code, http.StatusCreated)
}
if !strings.Contains(rec.Body.String(), `"title":"Learn Go"`) {
t.Errorf("body = %q, want the task title", rec.Body.String())
}
}
func TestCreateTaskInvalidJSON(t *testing.T) {
req := httptest.NewRequest(http.MethodPost, "/api/tasks", strings.NewReader("not json"))
rec := httptest.NewRecorder()
createTask(rec, req)
if rec.Code != http.StatusBadRequest {
t.Errorf("code = %d, want %d", rec.Code, http.StatusBadRequest)
}
}
To go further: add a check on the header to the first test, rec.Header().Get("Content-Type") must be application/json.
Show the solution
Writing the body sends the response header with the default status, 200: a WriteHeader called afterwards is ignored. The status must therefore be set before writing:
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(task)
Le serveur HTTP ne fonctionne pas dans le Playground ni dans un sandbox en ligne. Installez Go localement pour tester : https://go.dev/dl/
🎯 Pratique
S'entraîner (clique pour ouvrir) :
✨ Prompt IA
Copiez ce prompt dans Claude ou ChatGPT :
Crée une API REST Go complète pour gérer des livres (CRUD). Utilise uniquement la bibliothèque standard. Ajoute la validation des données et des codes de statut HTTP appropriés.
💬 Ré-explique sans regarder
Sans relire le code de l'IA : décrivez de mémoire ce que fait un handler POST qui crée une ressource, de la lecture du r.Body jusqu'à la réponse. Quels packages et quel code de statut utilisez-vous ?
json.NewDecoder(r.Body).Decode(&task) (package encoding/json) ; on vérifie l'erreur retournée et on répond http.StatusBadRequest (400) si le JSON est invalide ; en cas de succès on écrit Content-Type: application/json, on renvoie http.StatusCreated (201) avec w.WriteHeader, puis on encode la ressource créée avec json.NewEncoder(w).Encode(...). Le routage et la méthode viennent de net/http.⚖️ Juge le code de l'IA
L'IA vous propose ce handler POST. Votre rôle de relecteur : l'accepter tel quel ou le rejeter, et dire pourquoi.
func handleCreateTask(w http.ResponseWriter, r *http.Request) {
var task Task
json.NewDecoder(r.Body).Decode(&task)
task.ID = len(tasks) + 1
tasks = append(tasks, task)
json.NewEncoder(w).Encode(task)
}
Decode est ignorée : si le client envoie un JSON invalide, task reste vide et on enregistre quand même une tâche vide au lieu de répondre 400 Bad Request. Réflexe Go : on ne jette jamais une error retournée. Il faut if err := json.NewDecoder(r.Body).Decode(&task); err != nil { http.Error(w, "JSON invalide", http.StatusBadRequest); return }. Bonus : pensez aussi à w.WriteHeader(http.StatusCreated) avant d'encoder, sinon la réponse part en 200 alors qu'on a créé une ressource.🧠 Rappel libre
Sans remonter dans la leçon : quelle est la signature d'un handler HTTP en Go, et avec quelle fonction renvoyez-vous un struct au format JSON dans la réponse ?
func(w http.ResponseWriter, r *http.Request) : w sert à écrire la réponse, r contient la requête entrante. Pour renvoyer un struct en JSON, on pose d'abord l'en-tête w.Header().Set("Content-Type", "application/json") puis on appelle json.NewEncoder(w).Encode(monStruct) (ou json.Marshal qui retourne un []byte).Votre API REST répond en JSON, mais comment savoir qu'elle ne casse pas au prochain changement ? Dans la dernière leçon, vous écrivez des tests avec le package testing intégré et vous mesurez les performances avec les benchmarks.
Leçon 11 : Tests et benchmarks →