DEV Community

Discussion on: Daily Challenge #56 - Coffee Shop

Collapse
 
dak425 profile image
Donald Feury • Edited

Hello Cherny!

You can most certainly use floats as keys in a map, you just need to indicate what type the keys are in the map declaration.

For your code above you have:

map[string]string

If you want to use floats as the keys, you simply need to indicate as so:

map[float64]string

Then you could so something like this:

types := map[float64]string{
  2.2: "Americano",
  2.3: "Latte",
  2.4: "Flat White",
  3.5: "Filter",
}

That would also enable you to pass in a float as an argument to the func instead of a string as well.

Don't forget to include logic for checking if they have exact change!

If you would still like me to post a solution using a map I would be more than happy to.

Note

In Golang, if you attempt to access a value of a map with a key that doesn't exist, you will get the zero value of the type stored in the map. That may be useful for the exact change logic check in your solution.

If we use your map for example:

types[5.0]

would return "", since the key 5.0 doesn't exist and "" is the zero value for a string in Golang.