Back to listGo
Go HTTP Server with Gorilla Mux
Lv.5724@mukitaro6 playsDec 28, 2025
Go REST API using Gorilla Mux router. Popular Go web toolkit.
preview.go
1package main2 3import (4 "encoding/json"5 "log"6 "net/http"7 8 "github.com/gorilla/mux"9)10 11type User struct {12 ID string `json:"id"`13 Name string `json:"name"`14}15 16var users []User17 18func getUsers(w http.ResponseWriter, r *http.Request) {19 w.Header().Set("Content-Type", "application/json")20 json.NewEncoder(w).Encode(users)21}22 23func getUser(w http.ResponseWriter, r *http.Request) {24 params := mux.Vars(r)25 for _, user := range users {26 if user.ID == params["id"] {27 json.NewEncoder(w).Encode(user)28 return29 }30 }31 http.NotFound(w, r)32}33 34func main() {35 r := mux.NewRouter()36 r.HandleFunc("/users", getUsers).Methods("GET")37 r.HandleFunc("/users/{id}", getUser).Methods("GET")38 log.Fatal(http.ListenAndServe(":8080", r))39}Custom problems are not included in rankings