package main import ( "encoding/json" "net/http" "strconv" ) const ( version = "1.0.1" ) func main() { mux := http.NewServeMux() mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("Hello, World page")) }) mux.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(map[string]string{"message": "hello world json!"}) }) mux.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(map[string]string{"version": version}) }) mux.HandleFunc("/sum", func(w http.ResponseWriter, r *http.Request) { a, _ := strconv.Atoi(r.URL.Query().Get("a")) b, _ := strconv.Atoi(r.URL.Query().Get("b")) json.NewEncoder(w).Encode(map[string]int{"result": sum(a, b)}) }) mux.HandleFunc("/sub", func(w http.ResponseWriter, r *http.Request) { a, _ := strconv.Atoi(r.URL.Query().Get("a")) b, _ := strconv.Atoi(r.URL.Query().Get("b")) json.NewEncoder(w).Encode(map[string]int{"result": sub(a, b)}) }) http.ListenAndServe(":8080", mux) } func sum(a, b int) int { return a + b } func sub(a, b int) int { return a - b }