代码拉取完成,页面将自动刷新
package main
import (
"bufio"
"fmt"
"log"
"net/http"
)
func main() {
// get the book adventures of sherlock holmes
res, err := http.Get("http://www.gutenberg.org/cache/epub/1661/pg1661.txt")
if err != nil {
log.Fatal(err)
}
// scan the page
scanner := bufio.NewScanner(res.Body)
defer res.Body.Close()
// Set the split function for the scanning operation.
scanner.Split(bufio.ScanWords)
// Create map with a key of int
// and a value of another map
// with a key of string, which will be the word
// and a value of int, which will be the number of times the word occurs
buckets := make(map[int]map[string]int)
// Create slices to hold words words
for i := 0; i < 12; i++ {
buckets[i] = make(map[string]int)
}
// Loop over the words
for scanner.Scan() {
word := scanner.Text()
n := hashBucket(word, 12)
buckets[n][word]++
}
// Print words in a bucket
for k, v := range buckets[6] {
fmt.Println(v, " \t- ", k)
}
}
func hashBucket(word string, buckets int) int {
var sum int
for _, v := range word {
sum += int(v)
}
return sum % buckets
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。