How can I count the number of strings that occurs exactly once in a text file using GO? I have read some of the package description of golang, maybe I should use bufio.NewScanner to read the contents of the passed-in file a line at a time. Then I try to use map to count the occurrence of each string:
stringcount := make(map[string]int)
how can I update the data of this empty map? For example, if the first string is "hello" in the file, how to make stringcount["hello"] = 1?
Here is my try:
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func main() {
frequencyofWord := map[string]int{}
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
fmt.Println(scanner.Text())
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "reading standard input:", err)
}
s := strings.Fields(scanner.Text()) //one more question : is strings.Fields used correctly here?
countSingleton(s)
}
func countSingleton(a []string) {
//here how to update the map according to the text read ?//
}