1json.go 591 B

12345678910111213141516171819202122232425262728293031323334
  1. package main
  2. import (
  3. "database/sql"
  4. "encoding/json"
  5. "fmt"
  6. "net/http"
  7. _ "github.com/go-sql-driver/mysql"
  8. )
  9. type User struct {
  10. Name string `json:"name"`
  11. Email string `json:"email"`
  12. ID int `json:"int"`
  13. }
  14. func userRouter(w http.ResponseWriter, r *http.Request) {
  15. ourUser := User{
  16. Name: "Bill Smith",
  17. Email: "bill.smith@example.com",
  18. ID: 100,
  19. }
  20. output, _ := json.Marshal(&ourUser)
  21. fmt.Fprint(w, string(output))
  22. }
  23. var database *sql.DB
  24. func main() {
  25. fmt.Println("Starting JSON server")
  26. http.HandleFunc("/user", userRouter)
  27. http.ListenAndServe(":8080", nil)
  28. }