Last active
September 11, 2018 06:16
-
-
Save yukpiz/21935e472e65cbc33057a03aa0aa1a1c 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 ( | |
"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