Package · Hello World
GET /
Returns a JSON greeting message.
Endpoint
Responses
| Status | Description | |
|---|---|---|
| 200 | Request was successful | |
| ||
| 400 | Bad request | |
| 401 | Unauthorized | |
| 403 | Forbidden | |
| 404 | Not found | |
| 500 | Internal server error | |
Architecture Decision Records
- The API returns a JSON object containing a greeting message.
- The API is designed to be simple and easy to understand, making it a good starting point for learning how to build APIs.
Request Overview
Implementation
package main
import (
"encoding/json"
"net/http"
)
func main() {
http.HandleFunc("/", helloWorldHandler)
http.ListenAndServe(":8080", nil)
}
func helloWorldHandler(w http.ResponseWriter, r *http.Request) {
// Set the Content-Type header to application/json
w.Header().Set("Content-Type", "application/json")
// Create a map for the JSON response
response := map[string]string{"message": "Hello, World!"}
// Encode the map as JSON and write it to the response
json.NewEncoder(w).Encode(response)
}
hello-world.go
Contract
{
"openapi": "3.0.3",
"info": {
"title": "Hello World API",
"version": "1.0.0",
"description": "A simple example API that returns a greeting message."
},
"servers": [
{
"url": "https://hello-world.api.docs.squid-ware.nl"
}
],
"paths": {
"/": {
"get": {
"operationId": "getHelloWorld",
"summary": "Get greeting",
"description": "Returns a JSON object containing a greeting message.",
"responses": {
"200": {
"description": "Greeting response",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": ["message"],
"properties": {
"message": {
"type": "string",
"example": "Hello, World!"
}
}
},
"example": {
"message": "Hello, World!"
}
}
}
}
}
}
}
},
"components": {}
}
hello-world.contract.json