Skip to content

Instantly share code, notes, and snippets.

@JoeGlines
Created October 14, 2025 11:46
Show Gist options
  • Save JoeGlines/cc9ddf508fe4988893bf0f4ac44e5193 to your computer and use it in GitHub Desktop.
Save JoeGlines/cc9ddf508fe4988893bf0f4ac44e5193 to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"fmt"
"net/http"
)
type User struct {
ID int `json:"id"`
Name string `json:"name"`
Age int `json:"age"`
}
var users = []User{
{ID: 1, Name: "Alice", Age: 30},
{ID: 2, Name: "Bob", Age: 25},
}
func getUsers(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(users)
}
func main() {
http.HandleFunc("/api/users", getUsers)
fmt.Println("Server running on http://localhost:8080")
http.ListenAndServe(":8080", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment