2xml.go 602 B

1234567891011121314151617181920212223242526272829303132333435
  1. package main
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "net/http"
  6. "encoding/xml"
  7. _ "github.com/go-sql-driver/mysql"
  8. )
  9. type User struct {
  10. Name string `xml:"name"`
  11. Email string `xml:"email"`
  12. ID int `xml:"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, _ := xml.MarshalIndent(&ourUser, "", " ")
  21. fmt.Fprint(w, string(output))
  22. }
  23. var database *sql.DB
  24. func main() {
  25. fmt.Println("Starting XML server")
  26. http.HandleFunc("/user", userRouter)
  27. http.ListenAndServe(":8080", nil)
  28. }