Last active
September 14, 2015 17:10
-
-
Save hectorgool/9195c720c35ea92809d8 to your computer and use it in GitHub Desktop.
go run users.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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