"Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”
Install Go
Clone the repository into $GOPATH/src/github.com/
To check my project you can execute the scenario fizz_buzz/src$ ./scenario.sh
fizz_buzz/src/go$ go test -v
=== RUN TestFizzBuzz1
--- PASS: TestFizzBuzz1 (0.00s)
=== RUN TestFizzBuzz2
--- PASS: TestFizzBuzz2 (0.00s)
=== RUN TestFizzBuzzNaive1
--- PASS: TestFizzBuzzNaive1 (0.00s)
=== RUN TestFizzBuzzNaive2
--- PASS: TestFizzBuzzNaive2 (0.00s)
PASS
ok github.com/fizz_buzz/src/go 0.002s
Run with Go :
fizz_buzz/src/go$ go run fizz_buzz.go
listening...
Or execute binary :
fizz_buzz/bin$ ./fizz_buzz
listening...
Here an example of command curl :
curl -H "Content-Type: application/json" -XGET "http://localhost:8000/fizz_buzz/v1" -d '{"string1":"fizz","string2":"buzz","int1":3,"int2":5,"limit":15}'
My naming convention that I use is, if we take the variable "hello" for example :
g_hello
global variablel_hello
local variablec_hello
loop variablep_hello
parameter
Then I use snake_case convention, even if there is a collision with the test naming convention of Go.
My tests miss the REST API. They test the business part of fizz_buzz. The next step will be to mock the http.Request to implement the API tests. With these tests I should have a code coverage near of 100%.
Waiting that, I implemented a Bash script scenario to check my REST API with curl
.
I use dep to manage my dependencies.
fizz_buzz/src/go$ go test -bench=. -benchtime=60s
I wanted to initialize a benchmark with Go. So I write another version of fizz_buzz. I named it with the keyword "naive" because I thinking it was less optimize. My first benchmark didn't confirm my intuition. Indeed I believed reduce the amount of test but with a small int1
, the naive version will leave the if statements offen at the second. My first version, in all case, will execute all of its if statement.
The other point could be the string concatenation. I tried with bytes.Buffer
but the difference was not perceptible. This is something that I could more investigate later.
fizz_buzz/src/go$ go test -bench=. -benchtime=60s
goos: linux
goarch: amd64
pkg: github.com/fizz_buzz/src/go
BenchmarkFizzBuzz1-4 2000000 58235 ns/op
BenchmarkFizzBuzzNaive1-4 2000000 50160 ns/op
BenchmarkFizzBuzz2-4 1000000 64501 ns/op
BenchmarkFizzBuzzNaive2-4 1000000 70405 ns/op
PASS
ok github.com/fizz_buzz/src/go 462.988s
- Paris Simon