Welcome to The Coding College!
Practice is essential to master any programming language. This page provides a series of hands-on exercises tailored for beginners and intermediate learners in Go programming. These exercises will help you strengthen your understanding of core concepts and apply them in real-world scenarios.
Beginner-Level Exercises
Exercise 1: Print “Hello, Go!”
Write a program to print the message “Hello, Go!” to the console.
Solution
package main
import "fmt"
func main() {
fmt.Println("Hello, Go!")
}
Exercise 2: Add Two Numbers
Write a program to add two numbers and display the result.
Sample Output
Enter first number: 10
Enter second number: 20
Sum: 30
Solution
package main
import "fmt"
func main() {
var num1, num2 int
fmt.Print("Enter first number: ")
fmt.Scan(&num1)
fmt.Print("Enter second number: ")
fmt.Scan(&num2)
sum := num1 + num2
fmt.Println("Sum:", sum)
}
Exercise 3: Check Even or Odd
Write a program to check whether a number is even or odd.
Solution
package main
import "fmt"
func main() {
var num1, num2 int
fmt.Print("Enter first number: ")
fmt.Scan(&num1)
fmt.Print("Enter second number: ")
fmt.Scan(&num2)
sum := num1 + num2
fmt.Println("Sum:", sum)
}
Exercise 4: Find the Largest Number
Write a program to find the largest of three numbers.
Solution
package main
import "fmt"
func main() {
var num int
fmt.Print("Enter a number: ")
fmt.Scan(&num)
if num%2 == 0 {
fmt.Println("Even")
} else {
fmt.Println("Odd")
}
}
Intermediate-Level Exercises
Exercise 5: Factorial Using Recursion
Write a program to calculate the factorial of a number using recursion.
Solution
package main
import "fmt"
func factorial(n int) int {
if n == 0 {
return 1
}
return n * factorial(n-1)
}
func main() {
var num int
fmt.Print("Enter a number: ")
fmt.Scan(&num)
fmt.Printf("Factorial of %d is %d\n", num, factorial(num))
}
Exercise 6: Check for Palindrome String
Write a program to check if a given string is a palindrome.
Solution
package main
import (
"fmt"
"strings"
)
func isPalindrome(str string) bool {
str = strings.ToLower(str)
for i := 0; i < len(str)/2; i++ {
if str[i] != str[len(str)-1-i] {
return false
}
}
return true
}
func main() {
var input string
fmt.Print("Enter a string: ")
fmt.Scan(&input)
if isPalindrome(input) {
fmt.Println("Palindrome")
} else {
fmt.Println("Not a palindrome")
}
}
Exercise 7: Fibonacci Series
Write a program to print the first n
numbers in the Fibonacci series.
Solution
package main
import "fmt"
func fibonacci(n int) {
a, b := 0, 1
for i := 0; i < n; i++ {
fmt.Print(a, " ")
a, b = b, a+b
}
fmt.Println()
}
func main() {
var n int
fmt.Print("Enter the number of terms: ")
fmt.Scan(&n)
fibonacci(n)
}
Exercise 8: Count Word Frequency
Write a program to count the frequency of each word in a given string.
Solution
package main
import (
"fmt"
"strings"
)
func main() {
var text string
fmt.Println("Enter a sentence:")
fmt.Scanln(&text)
words := strings.Fields(strings.ToLower(text))
wordCount := make(map[string]int)
for _, word := range words {
wordCount[word]++
}
fmt.Println("Word Frequencies:")
for word, count := range wordCount {
fmt.Printf("%s: %d\n", word, count)
}
}
Advanced-Level Exercises
Exercise 9: Implement a Custom Stack
Write a program to implement a stack with push, pop, and peek operations.
Solution
package main
import "fmt"
type Stack struct {
elements []int
}
func (s *Stack) Push(value int) {
s.elements = append(s.elements, value)
}
func (s *Stack) Pop() int {
if len(s.elements) == 0 {
fmt.Println("Stack is empty")
return -1
}
value := s.elements[len(s.elements)-1]
s.elements = s.elements[:len(s.elements)-1]
return value
}
func (s *Stack) Peek() int {
if len(s.elements) == 0 {
fmt.Println("Stack is empty")
return -1
}
return s.elements[len(s.elements)-1]
}
func main() {
stack := &Stack{}
stack.Push(10)
stack.Push(20)
stack.Push(30)
fmt.Println("Top Element:", stack.Peek())
fmt.Println("Popped:", stack.Pop())
fmt.Println("Popped:", stack.Pop())
}
Exercise 10: Read File and Count Characters
Write a program to read a file and count the occurrences of each character.
Solution
package main
import (
"fmt"
"io/ioutil"
"log"
)
func main() {
content, err := ioutil.ReadFile("example.txt")
if err != nil {
log.Fatal(err)
}
charCount := make(map[rune]int)
for _, char := range string(content) {
charCount[char]++
}
fmt.Println("Character Frequencies:")
for char, count := range charCount {
fmt.Printf("%c: %d\n", char, count)
}
}
Conclusion
These exercises cover a wide range of Go topics to help you improve your coding skills. Try them yourself and explore more Go tutorials at The Coding College.