GoGo

Go exercises with solutions

Solve in editor

Write your solution right in the browser.

Open exercises with editor

1. Even numbers Beginner

Print all even numbers from 1 to 10 – each on its own line.

Expected output: 2 ⏎ 4 ⏎ 6 ⏎ 8 ⏎ 10

Note

In the for loop, count with i += 2 starting at 2, or check with i%2 == 0.

Show sample solution
package main

import "fmt"

func main() {
	for i := 2; i <= 10; i += 2 {
		fmt.Println(i)
	}
}

2. FizzBuzz (1–15) Beginner

Print the numbers 1 to 15. For multiples of 3 print Fizz instead, for multiples of 5 print Buzz, and for both print FizzBuzz.

Expected output: 1 ⏎ 2 ⏎ Fizz ⏎ 4 ⏎ Buzz ⏎ Fizz ⏎ 7 ⏎ 8 ⏎ Fizz ⏎ Buzz ⏎ 11 ⏎ Fizz ⏎ 13 ⏎ 14 ⏎ FizzBuzz

Note

Check the "divisible by 15" case first, then 3, then 5. A switch without an expression is ideal for this.

Show sample solution
package main

import "fmt"

func main() {
	for i := 1; i <= 15; i++ {
		switch {
		case i%15 == 0:
			fmt.Println("FizzBuzz")
		case i%3 == 0:
			fmt.Println("Fizz")
		case i%5 == 0:
			fmt.Println("Buzz")
		default:
			fmt.Println(i)
		}
	}
}

3. Reverse a word Beginner

Reverse the string "Hello" and print it.

Expected output: olleH

Note

Convert the string with []rune(wort), swap elements from both ends and convert back with string(...).

Show sample solution
package main

import "fmt"

func main() {
	wort := "Hello"
	r := []rune(wort)
	for i, j := 0, len(r)-1; i < j; i, j = i+1, j-1 {
		r[i], r[j] = r[j], r[i]
	}
	fmt.Println(string(r))
}

4. Sum of squares Beginner

Calculate the sum of the squares of the numbers 1 to 10 and print it.

Expected output: 385

Note

In a for loop, add up i*i in a variable summe.

Show sample solution
package main

import "fmt"

func main() {
	summe := 0
	for i := 1; i <= 10; i++ {
		summe += i * i
	}
	fmt.Println(summe)
}

5. Palindrome function Intermediate

Write a function istPalindrom(text string) bool that returns true if the text reads the same forwards and backwards (ignore case). Print istPalindrom("Anna") and istPalindrom("Python").

Expected output: true ⏎ false

Note

Convert with strings.ToLower and []rune and compare from the outside in with two indices.

Show sample solution
package main

import (
	"fmt"
	"strings"
)

func istPalindrom(text string) bool {
	r := []rune(strings.ToLower(text))
	for i, j := 0, len(r)-1; i < j; i, j = i+1, j-1 {
		if r[i] != r[j] {
			return false
		}
	}
	return true
}

func main() {
	fmt.Println(istPalindrom("Anna"))
	fmt.Println(istPalindrom("Python"))
}

6. Most frequent word Intermediate

Find the most frequent word in "a b a c b a" and print it.

Expected output: a

Note

Count with a map[string]int (strings.Fields splits the text) and, while iterating, remember the word with the highest count.

Show sample solution
package main

import (
	"fmt"
	"strings"
)

func main() {
	text := "a b a c b a"
	zaehler := map[string]int{}
	for _, w := range strings.Fields(text) {
		zaehler[w]++
	}
	beste := ""
	for w, n := range zaehler {
		if n > zaehler[beste] {
			beste = w
		}
	}
	fmt.Println(beste)
}

7. Primes up to 30 Intermediate

Print all prime numbers up to 30 on one line, separated by spaces.

Expected output: 2 3 5 7 11 13 17 19 23 29

Note

A number n is prime if no d with d*d <= n divides it. With fmt.Print and a flag erste you control the spaces; at the end call fmt.Println().

Show sample solution
package main

import "fmt"

func main() {
	erste := true
	for n := 2; n <= 30; n++ {
		prim := true
		for d := 2; d*d <= n; d++ {
			if n%d == 0 {
				prim = false
				break
			}
		}
		if prim {
			if !erste {
				fmt.Print(" ")
			}
			fmt.Print(n)
			erste = false
		}
	}
	fmt.Println()
}

8. Sum squares in parallel Advanced

Calculate the sum of the squares from 1 to 10 in parallel: start one goroutine per number that sends the square through a channel, and add up the ten results in main. Print the sum.

Expected output: 385

Note

In a loop, start go func(n int) { ch <- n * n }(i). Afterwards you receive exactly ten times with <-ch and add up.

Show sample solution
package main

import "fmt"

func main() {
	ch := make(chan int)
	for i := 1; i <= 10; i++ {
		go func(n int) {
			ch <- n * n
		}(i)
	}
	summe := 0
	for i := 0; i < 10; i++ {
		summe += <-ch
	}
	fmt.Println(summe)
}