Skip to content

Instantly share code, notes, and snippets.

Created February 9, 2018 07:01
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
package main
import (
var f finder
func findCountry(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
country, err := f.FindByName(params["id"])
if err != nil {
http.Error(w, "Invalid Country", http.StatusNotFound)
log.Printf("unable to find country with name: %s", params["id"])
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(country); err != nil {
log.Printf("error encoding: %v", err)
func main() {
r := mux.NewRouter()
r.HandleFunc("/country/{id}", findCountry).Methods("GET")
if err := http.ListenAndServe(":3000", r); err != nil {
type finder interface {
FindByName(string) (interface{}, error)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment