Last active
September 11, 2018 06:16
-
-
Save yukpiz/cf73ec16f403feb7991a44eb46a31748 to your computer and use it in GitHub Desktop.
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" | |
"github.com/julienschmidt/httprouter" | |
"log" | |
"net/http" | |
) | |
type Profile struct { | |
Name string `json:"name"` | |
Age int `json:"age"` | |
Gender string `json:"gender"` | |
FavoriteFoods []string `json:"favorite_foods"` | |
} | |
var bob Profile = Profile{ | |
Name: "Bob", | |
Age: 25, | |
Gender: "Man", | |
FavoriteFoods: []string{"Hamburger", "Cookie", "Chocolate"}, | |
} | |
var alice Profile = Profile{ | |
Name: "Alice", | |
Age: 24, | |
Gender: "Woman", | |
FavoriteFoods: []string{"Apple", "Orange", "Melon"}, | |
} | |
func GetProfile(w http.ResponseWriter, r *http.Request, p httprouter.Params) { | |
name := p.ByName("name") | |
var resProfile Profile | |
if name == "Bob" { | |
resProfile = bob | |
} else if name == "Alice" { | |
resProfile = alice | |
} else { | |
http.Error(w, fmt.Sprintf("%d Not Found", http.StatusNotFound), http.StatusNotFound) | |
return | |
} | |
bytes, err := json.Marshal(resProfile) | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusBadRequest) | |
} | |
fmt.Fprintf(w, string(bytes)) | |
} | |
func main() { | |
router := httprouter.New() | |
router.GET("/Profile/:name", GetProfile) | |
err := http.ListenAndServe(":8080", router) | |
if err != nil { | |
log.Fatal(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment