ex1_helloworld.go 422 B

123456789101112131415161718192021222324
  1. package main
  2. import (
  3. "net/http"
  4. "encoding/json"
  5. "fmt"
  6. )
  7. type API struct {
  8. Message string "json:message"
  9. }
  10. func main() {
  11. http.HandleFunc("/api", func(w http.ResponseWriter, r *http.Request) {
  12. message := API{Message:"Hello, world"}
  13. output, err := json.Marshal(message)
  14. if err != nil {
  15. fmt.Println("Something went wrong", err)
  16. }
  17. fmt.Fprintf(w, string(output))
  18. });
  19. http.ListenAndServe(":8080", nil)
  20. }