Mode:
Duration:
1
Coding works best on desktop or with an external keyboard.
Coding works best on desktop or with an external keyboard.
Demonstrates a simple Martini application with routes for listing and creating Todo items.
package main
import (
"github.com/go-martini/martini"
"net/http"
"encoding/json"
)
type Todo struct {
ID int `json:"id"`
Title string `json:"title"`
Completed bool `json:"completed"`
}
var todos []Todo
func main() {
m := martini.Classic()
m.Get("/todos", func(res http.ResponseWriter) {
json.NewEncoder(res).Encode(todos)
})
m.Post("/todos", func(req *http.Request, res http.ResponseWriter) {
var todo Todo
json.NewDecoder(req.Body).Decode(&todo)
todos = append(todos, todo)
res.WriteHeader(http.StatusCreated)
json.NewEncoder(res).Encode(todo)
})
m.Run()
}Martini is a lightweight web framework for Go, designed for rapid development with simplicity and minimal boilerplate.
Origin & Creator
Created by Codegangsta (now known as Unrolled) in 2011, originally popularized in the Go community.
Industrial Note
Martini is favored for small to medium Go web services, APIs, and microservices where simplicity and rapid development outweigh large-scale ecosystem needs.