DEV Community

Discussion on: Daily Challenge #56 - Coffee Shop

chachan profile image

I used this challenge as my "hello world" with Go. At first I tried a map thinking that it might be easy to have float64 type as key. Can you please post a solution using a map? Here's mine:

package main

import "fmt"

func buy(amount string) string {
    types := map[string]string{
        "2.2": "Americano",
        "2.3": "Latte",
        "2.4": "Flat white",
        "3.5": "Filter",
    return fmt.Sprintf("Here is your %s have a nice day!", types[amount])

func main() {
dak425 profile image
Donald Feury • Edited on

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:


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


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.


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:


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