Skip to content

Instantly share code, notes, and snippets.

@hectorgool
Last active September 14, 2015 17:10
Show Gist options
  • Select an option

  • Save hectorgool/9195c720c35ea92809d8 to your computer and use it in GitHub Desktop.

Select an option

Save hectorgool/9195c720c35ea92809d8 to your computer and use it in GitHub Desktop.
go run users.go
package main
import (
"encoding/json"
"fmt"
"net/http"
)
//In a struct, a third parameter in a variable/type declaration is called a tag
type User struct {
Name string `json:"name"`
Email string `json:"email"`
ID int `json:"int"`
}
func userRouter(w http.ResponseWriter, r *http.Request) {
ourUser := User{}
ourUser.Name = "Rodolfo Guzman"
ourUser.Email = "santo@elsanto.com"
ourUser.ID = 666
output, _ := json.Marshal(&ourUser)
fmt.Fprintln(w, string(output))
}
func main() {
fmt.Println("Starting JSON server")
http.HandleFunc("/user", userRouter)
http.ListenAndServe(":8080", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment