Created
October 14, 2025 11:46
-
-
Save JoeGlines/cc9ddf508fe4988893bf0f4ac44e5193 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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