Open15

Goを勉強する

てぃーてぃーてぃーてぃー

なんかできた

package main

import (
	"net/http"
	"encoding/json"
	"github.com/go-chi/chi/v5"
	"github.com/go-chi/chi/v5/middleware"
)

type Response struct {
	Message string `json:"message"`
}

func main() {
	r := chi.NewRouter()
	r.Use(middleware.RequestID)
	r.Use(middleware.Logger)
	r.Use(middleware.Recoverer)

	r.Get("/ping", func(w http.ResponseWriter, r *http.Request) {
		response := &Response{Message: "hello"}
		b, err := json.Marshal(response)
		if err != nil {
			w.Write([]byte("an error occured"))
		}

		w.Header().Set("Content-Type", "application/json")
		w.Write(b)
	})

	http.ListenAndServe(":3333", r)
}