Hello World Example
Your first Lokstra application - the simplest possible API
Related to: Quick Start Guide
๐ What This Example Shows
- โ Creating a router
- โ Registering simple GET routes
- โ Returning strings and maps (auto JSON)
- โ Running the app
๐ Run the Example
# From this directory
go run main.go
Server will start on http://localhost:3000
๐งช Test the Endpoints
Option 1: Using test.http (VS Code REST Client)
Open test.http in VS Code and click โSend Requestโ above each request.
Option 2: Using curl
# Hello endpoint
curl http://localhost:3000/
# Ping endpoint
curl http://localhost:3000/ping
# Time endpoint (returns JSON)
curl http://localhost:3000/time
๐ Expected Results
GET /
{
"status": "success",
"data": "Hello, Lokstra!"
}
GET /ping
{
"status": "success",
"data": "pong"
}
GET /time
{
"status": "success",
"data": {
"datetime": "2025-10-15T02:22:27+07:00",
"timestamp": 1760469747
}
}
๐ก Key Concepts
1. Router Creation
r := lokstra.NewRouter("api")
Creates a new router named โapiโ
2. Simple Handler Forms
// Form 1: Return string
r.GET("/", func() string {
return "Hello, Lokstra!"
})
// Form 2: Return map (auto converts to JSON)
r.GET("/time", func() map[string]any {
return map[string]any{
"timestamp": time.Now().Unix(),
}
})
3. Running the App
app := lokstra.NewApp("hello", ":3000", r)
app.Run(30 * time.Second) // 30s graceful shutdown timeout
๐ Whatโs Next?
Try modifying:
- Add more routes
- Return different data types
- Change the port
See more examples:
- Handler Forms - All 29 handler variations
- CRUD API - Full REST API with services
- Multi-Deployment - Monolith vs Microservices
Questions? Check the Quick Start Guide