I am just trying to print the output of map function...
Input: "hello world" Output expected: map['h': 1, 'e': 1, 'l': 3, 'o': 2, 'r': 1, 'w': 1, 'd': 1, ' ': 1] My Code:
package main
import "fmt"
/*
https://www.programmingexpert.io/programming-with-go/maps/practice-questions/1
*/
func main() {
CharacterFrequency("hello world")
}
func CharacterFrequency(sentence string) map[rune]int {
characters := map[rune]int{}
// characters := make(map[rune]int)
for _, char := range sentence {
value, ok := characters[char]
if !ok {
characters[char] = 1
} else {
characters[char] = value + 1
}
}
//fmt.Println(characters)
//fmt.Printf("%c", characters)
fmt.Printf("%c", map[rune]int) // so here is where I am not getting proper values
return characters
}
//fmt.Println(characters)
//fmt.Printf("%c", characters)
fmt.Printf("%c", map[rune]int) // so here is where I am not getting proper values
expecting: map['h': 1, 'e': 1, 'l': 3, 'o': 2, 'r': 1, 'w': 1, 'd': 1, ' ': 1]
fmt.Println(characters) gave me below map[32:1 100:1 101:1 104:1 108:3 111:2 114:1 119:1]
fmt.Printf("%c", characters) gave me below map[ :☺ d:☺ e:☺ h:☺ l:♥ o:☻ r:☺ w:☺]
fmt.Printf("%c", map[rune]int) blows up with ".\10_go_map_dict_q1.go:29:19: map[rune]int (type) is not an expression"
package main
import "fmt"
func main() {
CharacterFrequency("hello world")
}
func CharacterFrequency(sentence string) map[string]int {
characters := map[string]int{}
for _, char := range sentence {
characters[string(char)]++
}
fmt.Printf("%v", characters)
// Output: map[ :1 d:1 e:1 h:1 l:3 o:2 r:1 w:1]
return characters
}