Skip to content

Instantly share code, notes, and snippets.

@yukpiz
Last active September 11, 2018 06:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yukpiz/cf73ec16f403feb7991a44eb46a31748 to your computer and use it in GitHub Desktop.
Save yukpiz/cf73ec16f403feb7991a44eb46a31748 to your computer and use it in GitHub Desktop.
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