servidor web de Golang
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
)
func main() {
http.Handle("/", http.HandlerFunc(home))
http.Handle("/example", http.HandlerFunc(example))
http.ListenAndServe(":5000", nil)
}
func example(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusCreated)
w.Header().Set("Content-Type", "application/json")
resp := make(map[string]string)
resp["message"] = "Status Created"
jsonResp, err := json.Marshal(resp)
if err != nil {
log.Fatalf("Error happened in JSON marshal. Err: %s", err)
}
w.Write(jsonResp)
return
}
func home(w http.ResponseWriter, r *http.Request) {
file_data, err := ioutil.ReadFile("./index.html")
if err != nil {
fmt.Println(err)
}
w.Write(file_data)
return
}
Undefined