1 Star 1 Fork 0

湖底观景 / GolangTraining

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
main.go 1.06 KB
一键复制 编辑 原始数据 按行查看 历史
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
}
1
https://gitee.com/zhangjianGood/GolangTraining.git
git@gitee.com:zhangjianGood/GolangTraining.git
zhangjianGood
GolangTraining
GolangTraining
afa19f5c43f3

搜索帮助