fix unused import
This commit is contained in:
parent
b8b3b180a3
commit
b8a0e3cde4
21
main.go
21
main.go
@ -2,7 +2,6 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
@ -14,15 +13,6 @@ type Context struct {
|
|||||||
R *http.Request
|
R *http.Request
|
||||||
}
|
}
|
||||||
|
|
||||||
// JSON serializes the given data to JSON and writes it to the response.
|
|
||||||
func (c *Context) JSON(statusCode int, data interface{}) {
|
|
||||||
c.W.Header().Set("Content-Type", "application/json")
|
|
||||||
c.W.WriteHeader(statusCode)
|
|
||||||
if err := json.NewEncoder(c.W).Encode(data); err != nil {
|
|
||||||
http.Error(c.W, err.Error(), http.StatusInternalServerError)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// HandlerFunc defines the handler used by the framework.
|
// HandlerFunc defines the handler used by the framework.
|
||||||
type HandlerFunc func(c *Context)
|
type HandlerFunc func(c *Context)
|
||||||
|
|
||||||
@ -73,7 +63,6 @@ func main() {
|
|||||||
engine := New()
|
engine := New()
|
||||||
|
|
||||||
engine.router.GET("/", sayHelloHandler)
|
engine.router.GET("/", sayHelloHandler)
|
||||||
engine.router.GET("/json", sayHelloJSONHandler)
|
|
||||||
|
|
||||||
slog.Info("Server is running at http://localhost:8080")
|
slog.Info("Server is running at http://localhost:8080")
|
||||||
http.ListenAndServe(":8080", engine)
|
http.ListenAndServe(":8080", engine)
|
||||||
@ -83,13 +72,3 @@ func sayHelloHandler(c *Context) {
|
|||||||
slog.Info("called sayHelloHandler")
|
slog.Info("called sayHelloHandler")
|
||||||
c.W.Write([]byte("Hello, World!"))
|
c.W.Write([]byte("Hello, World!"))
|
||||||
}
|
}
|
||||||
|
|
||||||
type Something struct {
|
|
||||||
Name string `json:"name"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func sayHelloJSONHandler(c *Context) {
|
|
||||||
slog.Info("called sayHelloJSONHandler")
|
|
||||||
something := Something{Name: "something"}
|
|
||||||
c.JSON(http.StatusOK, something)
|
|
||||||
}
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user