Hello World Example

Your first Lokstra application - the simplest possible API

Related to: Quick Start Guide


๐Ÿ“– What This Example Shows


๐Ÿš€ 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:

See more examples:


Questions? Check the Quick Start Guide