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/21935e472e65cbc33057a03aa0aa1a1c to your computer and use it in GitHub Desktop.
Save yukpiz/21935e472e65cbc33057a03aa0aa1a1c to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"github.com/julienschmidt/httprouter"
"log"
"net/http"
"strconv"
)
func FizzBuzz(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
num, err := strconv.Atoi(p.ByName("num"))
if err != nil || num < 1 {
http.Error(w, fmt.Sprintf("%d Bad Request", http.StatusBadRequest), http.StatusBadRequest)
return
}
var str string
for i := 1; i <= num; i++ {
if i%15 == 0 {
str += fmt.Sprintf("%d: %s\n", i, "FizzBuzz!")
} else if i%5 == 0 {
str += fmt.Sprintf("%d: %s\n", i, "Buzz")
} else if i%3 == 0 {
str += fmt.Sprintf("%d: %s\n", i, "Fizz")
} else {
str += fmt.Sprintf("%d:\n", i)
}
}
fmt.Fprintf(w, str)
}
func main() {
router := httprouter.New()
router.GET("/FizzBuzz/:num", FizzBuzz)
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