package main import ( "encoding/json" "net/http" "github.com/gorilla/mux" "log" ) type User struct { ID int `json:"id"` Name string `json:"name"` } func GetUsers(w http.ResponseWriter, r *http.Request){ //Place holder values users := []User{ {ID:1, Name:"John Doe"}, {ID:2, Name:"John Doe"}, } w.Header().Set("Content-Type", "application/json"); json.NewEncoder(w).Encode(users); } func GetUser(w http.ResponseWriter, r *http.Request){ vars := mux.Vars(r) //Grabs var from the route variable id := vars["id"] user := User{ID:1, Name:"John Doe"} //Placeholder if(id=="1"){ w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(user) }else{ http.Error(w, "User not found", http.StatusNotFound) } } func main(){ r := mux.NewRouter() r.HandleFunc("/users", GetUsers).Methods("GET") r.HandleFunc("/users/{id}", GetUsers).Methods("GET") log.Println("Server listening on port 8000"); http.ListenAndServe(":8000", r) }