Go HTTP Server
Go is known for its simplicity, performance, and excellent concurrency support.
Basic HTTP Server
package main
import (
"encoding/json"
"net/http"
"github.com/gorilla/mux"
)
func main() {
router := mux.NewRouter()
router.HandleFunc("/api/users", getUsers).Methods("GET")
router.HandleFunc("/api/users", createUser).Methods("POST")
http.ListenAndServe(":8000", router)
}
func getUsers(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(users)
}
Best Practices
- Use goroutines for concurrency
- Handle errors properly
- Use context for cancellation
- Implement proper logging
- Use middleware for common tasks