Answer the question
In order to leave comments, you need to log in
Why doesn't Debug work in GoLand?
Here is the code I have
package main
import (
"github.com/go-chi/chi"
"log"
"net/http"
"os"
"os/signal"
)
var DB map[string]string
func main() {
stopchan := make(chan os.Signal)
router := chi.NewRouter()
router.Route("/", func(r chi.Router) {
r.Get("/", GetIndexHandler)
r.Post("/", GetPostIndexHandler)
})
go func() {
err := http.ListenAndServe(":8080", router)
log.Fatal(err)
}()
signal.Notify(stopchan, os.Kill, os.Interrupt)
<-stopchan
}
func GetIndexHandler(w http.ResponseWriter, r *http.Request) {
key := chi.URLParam(r, "key")
value, exists := DB[key]
if exists {
w.Write([]byte(value))
} else {
w.WriteHeader(http.StatusNotFound)
}
}
func GetPostIndexHandler(w http.ResponseWriter, r *http.Request) {
key := chi.URLParam(r, "key")
value := chi.URLParam(r, "value")
DB[key] = value
w.WriteHeader(http.StatusCreated)
w.Write([]byte(value))
}
Answer the question
In order to leave comments, you need to log in
Because your application crashes on startup. And even an error displays that port 8080 is busy.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question