Playing around with a bit of Golang
This repo is contains implementations to exercises proposed in An Introduction to Programming in Go which I've found interesting to share.
fizzbuzz.go
- Write a program that prints out all the numbers evenly divisible by 3 between 1 and 100. (3, 6, 9, etc.)
for i := 0; i < 100; i++ {
if i%3 == 0 {
fmt.Println(i)
}
}
- 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".
for i := 0; i < 100; i++ {
fmt.Println(FizzBuzz(i))
}
min.go
- Write a program that finds the smallest number in this list:
x := []int{
48, 96, 86, 68,
57, 82, 63, 70,
37, 34, 83, 27,
19, 97, 9, 17,
}
fmt.Println(Min(x, x[0]))
half.go
-
Write a function which takes an integer and halves it and returns
true
if it was even orfalse
if it was odd. For example,half(1)
should return(0, false)
andhalf(2)
should return(1, true)
. -
Write a function with one variadic parameter that finds the greatest number in a list of numbers.
func greatest(numbers ...int) int {
max := numbers[0]
for _, number := range numbers {
if number > max {
max = number
}
}
return max
}
-
Write a
makeOddGenerator
function that generates odd numbers. -
Write a Fibonacci sequence function that can find
fib(n)
.
swap.go
- Write a program that can swap two integers (
x := 1; y := 2; swap(&x, &y)
should give youx=2
andy=1
).
func swap(x, y *int) {
tmp := new(int)
*tmp = *x
*x = *y
*y = *tmp
}
- Add a
perimeter
method to theShape
interface which calculates the perimeter of a shape. Implement the method forCircle
andRectangle
.