I've just started my study IT and I thought it would be fun to make a little program that would show you what day it is tomorrow. Sadly I'm stuck. Currently it's working when you write the correct number from the array, but I would like it to work with a string. So when you write 'Maandag' (monday in Dutch), the program will answer Dinsdag (Tuesday in Dutch)
This is my code so far:
package main
import (
"fmt"
)
func main() {
var counter int
var dag [7]string
dag[0] = "Zondag"
dag[1] = "Maandag"
dag[2] = "Dinsdag"
dag[3] = "Woensdag"
dag[4] = "Donderdag"
dag[5] = "Vrijdag"
dag[6] = "Zaterdag"
fmt.Println("Welke dag is het?")
fmt.Scan(&counter)
if counter == 6 {
counter = 0
fmt.Println(dag[counter])
}
if counter != 6 {
counter++
fmt.Println(dag[counter])
}
}
What are you looking for are enums. In Go they can be implemented like this:
type Weekday int
const (
Sunday Weekday = iota
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
)
func (day Weekday) String() string {
// declare an array of strings
// ... operator counts how many
// items in the array (7)
names := [...]string{
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"}
// → `day`: It's one of the
// values of Weekday constants.
// If the constant is Sunday,
// then day is 0.
// prevent panicking in case of
// `day` is out of range of Weekday
if day < Sunday || day > Saturday {
return "Unknown"
}
// return the name of a Weekday
// constant from the names array
// above.
return names[day]
}
// will display "Sunday"
fmt.Println(Sunday)
// will display "Monday"
fmt.Println(Sunday + 1)
If you do not need int underlying type, you can create it like this:
const (
Sunday = "Sunday"
//...
)
Range over the array of days to get the value of its index. Check for the string passed and fetch the value of matched string index. Then use that index to get the value of next day:
package main
import (
"fmt"
)
var (
counter int
day string
)
func main() {
var dag [7]string
dag[0] = "Zondag"
dag[1] = "Maandag"
dag[2] = "Dinsdag"
dag[3] = "Woensdag"
dag[4] = "Donderdag"
dag[5] = "Vrijdag"
dag[6] = "Zaterdag"
fmt.Println("Welke dag is het?")
fmt.Scan(&day)
for key, value := range dag {
if day == value {
counter = key
}
}
fmt.Println(counter)
if counter == 6 {
counter = 0
fmt.Println(dag[counter])
}
if counter != 6 {
counter = counter + 1
fmt.Println(dag[counter])
}
}
Or as Peter suggested you can use maps also which is more convenient and easy to use in this case:
package main
import (
"fmt"
)
func main() {
var value string
dag := make(map[string]string)
dag["Zondag"] = "Maandag"
dag["Maandag"] = "Dinsdag"
dag["Dinsdag"] = "Woensdag"
dag["Woensdag"] = "Donderdag"
dag["Donderdag"] = "Vrijdag"
dag["Vrijdag"] = "Zaterdag"
fmt.Println("Welke dag is het?")
fmt.Scan(&value)
fmt.Println(dag[value])
}
Working code on Go Playground
you can enumerate the array and transfer the number and the string
func GetNextDay(someday string){
for i, v := range dag{
if v == someday {
if i==6 {
i = 0
}else {
i = i + 1
}
return dag[i]
}
}
return "no such day in a week"
}