In this exercise, you'll learn how to use switch statements and functions in Go to handle days of the week dynamically.
We will create a program that:
Before implementing the functionality, we need a menu that allows users to choose options. This menu will loop until the user chooses to quit.
package main
import (
"fmt"
)
// Function to display the menu
func displayMenu() {
fmt.Println("\nSelect an option:")
fmt.Println("1. Enter a number (1-7) to get the day name")
fmt.Println("2. Check if a day is a weekday or weekend")
fmt.Println("3. Show all days of the week")
fmt.Println("4. Quit")
}
func main() {
var choice int
for {
displayMenu()
fmt.Print("Enter your choice: ")
fmt.Scanln(&choice)
switch choice {
case 1:
fmt.Println("Feature: Get day name - To be implemented")
case 2:
fmt.Println("Feature: Check if weekday/weekend - To be implemented")
case 3:
fmt.Println("Feature: Show all days - To be implemented")
case 4:
fmt.Println("Goodbye!")
return
default:
fmt.Println("Invalid choice! Please enter a number between 1 and 4.")
}
}
}
func getDayName(day int) string {
switch day {
case 1:
return "Monday"
case 2:
return "Tuesday"
case 3:
return "Wednesday"
case 4:
return "Thursday"
case 5:
return "Friday"
case 6:
return "Saturday"
case 7:
return "Sunday"
default:
return "Invalid day number! Please enter a number between 1 and 7."
}
}
func isWeekend(day int) string {
switch day {
case 6, 7:
return "It's a weekend!"
case 1, 2, 3, 4, 5:
return "It's a weekday."
default:
return "Invalid day number! Please enter a number between 1 and 7."
}
}
func showAllDays() {
days := []string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}
fmt.Println("\nDays of the week:")
for i, day := range days {
fmt.Printf("%d. %s\n", i+1, day)
}
}
package main
import (
"fmt"
)
func displayMenu() {
fmt.Println("\nSelect an option:")
fmt.Println("1. Enter a number (1-7) to get the day name")
fmt.Println("2. Check if a day is a weekday or weekend")
fmt.Println("3. Show all days of the week")
fmt.Println("4. Quit")
}
func getDayName(day int) string {
switch day {
case 1:
return "Monday"
case 2:
return "Tuesday"
case 3:
return "Wednesday"
case 4:
return "Thursday"
case 5:
return "Friday"
case 6:
return "Saturday"
case 7:
return "Sunday"
default:
return "Invalid day number! Please enter a number between 1 and 7."
}
}
func isWeekend(day int) string {
switch day {
case 6, 7:
return "It's a weekend!"
case 1, 2, 3, 4, 5:
return "It's a weekday."
default:
return "Invalid day number! Please enter a number between 1 and 7."
}
}
func showAllDays() {
days := []string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}
fmt.Println("\nDays of the week:")
for i, day := range days {
fmt.Printf("%d. %s\n", i+1, day)
}
}
func main() {
var choice int
for {
displayMenu()
fmt.Print("Enter your choice: ")
fmt.Scanln(&choice)
switch choice {
case 1:
fmt.Print("Enter a number (1-7): ")
var day int
fmt.Scanln(&day)
fmt.Println("The day is:", getDayName(day))
case 2:
fmt.Print("Enter a number (1-7): ")
var day int
fmt.Scanln(&day)
fmt.Println(isWeekend(day))
case 3:
showAllDays()
case 4:
fmt.Println("Goodbye!")
return
default:
fmt.Println("Invalid choice! Please enter a number between 1 and 4.")
}
}
}
🚀 Thanks for using these exercises! If you find this content helpful and want to support my work, buying me a coffee would be greatly appreciated! ☕😊