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:
packagemainimport"fmt"funcbuy(amountstring)string{types:=map[string]string{"2.2":"Americano","2.3":"Latte","2.4":"Flat white","3.5":"Filter",}returnfmt.Sprintf("Here is your %s have a nice day!",types[amount])}funcmain(){fmt.Println(buy("2.2"))fmt.Println(buy("3.5"))}
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.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
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:
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:
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:
would return "", since the key 5.0 doesn't exist and "" is the zero value for a string in Golang.