Serveur HTTP
Contexte
Section intitulée « Contexte »Le package net/http de Go facilite la construction de serveurs HTTP. Utilisez http.HandleFunc et http.ListenAndServe.
Un serveur simple qui répond avec “Bonjour, Monde”.
Code exemple
Section intitulée « Code exemple »package main
import ( "fmt" "net/http")
func handler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Bonjour, %s !", r.URL.Path[1:])}
func main() { http.HandleFunc("/", handler) http.ListenAndServe(":8080", nil)}Sortie (curl http://localhost:8080/Alice)
Section intitulée « Sortie (curl http://localhost:8080/Alice) »Bonjour, Alice !