create a map with a value of type float in go

To create a map with a value of type float, you can use the make function in Golang.

main.go
myMap := make(map[keyType]float64)
35 chars
2 lines

Replace keyType with the type of keys you want to use in your map. The float64 type indicates that the values in the map should be of type float.

Here is an example of creating a map with float values:

main.go
package main

import "fmt"

func main() {
    myMap := make(map[string]float64)

    myMap["apple"] = 2.99
    myMap["banana"] = 1.50
    myMap["orange"] = 0.80

    fmt.Println(myMap) // Output: map[apple:2.99 banana:1.5 orange:0.8]
}
236 chars
14 lines

In this example, we created a map with string keys and float values. We then added some key-value pairs to the map and printed the map to the console. You can also initialize a map with some values using a map literal like this:

main.go
myMap := map[string]float64{
            "apple":  2.99,
            "banana": 1.50,
            "orange": 0.80,
         }
124 chars
6 lines

The output will be the same as the previous example: map[apple:2.99 banana:1.5 orange:0.8]

related categories

gistlibby LogSnag