当前位置: 首页 > news >正文

网站备案与域名备案专业搜索引擎seo服务商

网站备案与域名备案,专业搜索引擎seo服务商,注册app,网站jianshe以下是第五周复合类型(数组、切片与映射)的详细学习内容,按照第四周的深度要求设计: 第五周:复合类型与词频统计 一、复合类型详解 1. 数组(Array) // 声明与初始化 var arr1 [3]int …

以下是第五周复合类型(数组、切片与映射)的详细学习内容,按照第四周的深度要求设计:


第五周:复合类型与词频统计


一、复合类型详解

1. 数组(Array)
// 声明与初始化
var arr1 [3]int           // 声明:[0 0 0]
arr2 := [3]string{"A", "B", "C"}  // 显式初始化
arr3 := [...]int{1,2,3}   // 编译器推断长度// 特性:
// - 固定长度(长度是类型的一部分)
// - 值传递(赋值/传参产生副本)
// - 内存连续分配// 操作示例:
arr := [5]int{10,20,30,40,50}
fmt.Println(arr[1])       // 20
arr[2] = 35               // 修改元素
fmt.Println(len(arr))     // 5(长度)
fmt.Println(cap(arr))     // 5(容量)
2. 切片(Slice)
// 创建方式
s1 := make([]int, 3, 5)   // 类型,长度,容量
s2 := []float64{1.1, 2.2} // 字面量
s3 := arr[1:3]            // 从数组切割// 特性:
// - 动态大小(自动扩容)
// - 引用类型(底层数组视图)
// - 包含ptr/len/cap三元组// 操作示例:
s := []int{10,20,30}
s = append(s, 40)         // 扩容追加
copy(s[1:], s[2:])        // 删除元素(20)
s = s[:len(s)-1]          // 新长度:[10 30 40]
3. 映射(Map)
// 初始化方式
m1 := make(map[string]int)
m2 := map[string]float64{"pi": 3.1415,"e":  2.7182,
}// 特性:
// - 无序键值对集合
// - 引用类型
// - 线程不安全// 操作示例:
ages := map[string]int{"Alice": 25,"Bob":   30,
}
ages["Charlie"] = 28      // 添加/修改
delete(ages, "Bob")       // 删除
if age, ok := ages["David"]; !ok {fmt.Println("不存在")
}

二、词频统计任务

需求分析
  1. 输入一段英文文本
  2. 输出单词出现频率(不区分大小写)
  3. 过滤标点符号和数字
  4. 支持并发处理(可选优化)
版本1:基础实现
func wordFrequency(text string) map[string]int {// 清理文本cleaner := func(r rune) rune {if unicode.IsLetter(r) {return unicode.ToLower(r)}return ' ' // 非字母转为空格}cleaned := strings.Map(cleaner, text)// 分割单词words := strings.Fields(cleaned)// 统计频率freq := make(map[string]int)for _, word := range words {freq[word]++}return freq
}
版本2:并发优化
func concurrentWordFrequency(text string) map[string]int {// 文本预处理(同上)cleaner := func(r rune) rune {/* 同版本1 */}cleaned := strings.Map(cleaner, text)words := strings.Fields(cleaned)// 并发处理var mu sync.Mutexvar wg sync.WaitGroupfreq := make(map[string]int)chunkSize := 1000for i := 0; i < len(words); i += chunkSize {end := i + chunkSizeif end > len(words) {end = len(words)}chunk := words[i:end]wg.Add(1)go func(words []string) {defer wg.Done()localFreq := make(map[string]int)for _, w := range words {localFreq[w]++}mu.Lock()for k, v := range localFreq {freq[k] += v}mu.Unlock()}(chunk)}wg.Wait()return freq
}

三、测试与性能

1. 表格驱动测试
func TestWordFrequency(t *testing.T) {tests := []struct {input stringwant  map[string]int}{{"Hello hello world",map[string]int{"hello":2, "world":1},},{"Go! 100% Go...",map[string]int{"go":2},},{"A man a plan a canal: Panama",map[string]int{"a":3, "man":1, "plan":1, "canal":1, "panama":1},},}for _, tt := range tests {got := wordFrequency(tt.input)if !reflect.DeepEqual(got, tt.want) {t.Errorf("输入 %q\n期望 %v\n实际 %v", tt.input, tt.want, got)}}
}
2. 性能基准测试
# 运行测试
go test -bench . -benchmem# 预期结果:
BenchmarkWordFrequency-8             5000    324521 ns/op   138920 B/op    502 allocs/op
BenchmarkConcurrent-8                8000    198745 ns/op   189654 B/op   1502 allocs/op

四、进阶技巧

1. 内存优化(预分配)
// 预估容量减少扩容
words := make([]string, 0, len(text)/5)  // 按平均单词长度5预估
freq := make(map[string]int, 1000)       // 预分配哈希表槽位
2. 正则表达式优化
// 使用正则分割更高效
re := regexp.MustCompile(`\W+`)
words := re.Split(strings.ToLower(text), -1)
3. 自定义排序输出
func sortedFrequency(freq map[string]int) []string {type pair struct {word  stringcount int}pairs := make([]pair, 0, len(freq))for k, v := range freq {pairs = append(pairs, pair{k, v})}sort.Slice(pairs, func(i, j int) bool {return pairs[i].count > pairs[j].count})result := make([]string, len(pairs))for i, p := range pairs {result[i] = fmt.Sprintf("%s:%d", p.word, p.count)}return result
}

五、扩展练习

  1. 停用词过滤

    func filterStopWords(freq map[string]int, stopWords map[string]struct{}) {for w := range freq {if _, exists := stopWords[w]; exists {delete(freq, w)}}
    }
    
  2. 词云生成器

    func generateWordCloud(freq map[string]int, size int) []string {// 根据频率生成不同字号标记// 示例:["GO(12)", "语言(8)", "并发(20)"]
    }
    

六、学习检查清单

  • 能正确定义数组、切片和映射
  • 理解切片扩容机制(容量翻倍策略)
  • 会使用sync.Mutex处理并发map访问
  • 能解释数组与切片的底层关系
  • 理解map的哈希表实现原理
  • 会进行切片的内存预分配优化
  • 能处理Unicode字符的文本清洗
  • 会编写并发安全的统计程序

通过本学习内容,您将掌握Go语言核心复合类型的特性和高效使用方法,并能够根据实际场景选择最佳数据结构。建议:

  1. 尝试处理1GB以上的大文本文件
  2. 比较不同分块策略对并发版本的影响
  3. 使用pprof分析内存分配热点
  4. 实现扩展练习中的词云可视化功能

文章转载自:
http://dinncotannia.bkqw.cn
http://dinncobabassu.bkqw.cn
http://dinncohouseboy.bkqw.cn
http://dinncosteeple.bkqw.cn
http://dinncoarchil.bkqw.cn
http://dinncoperpetually.bkqw.cn
http://dinncoimplosion.bkqw.cn
http://dinncoaqualung.bkqw.cn
http://dinncosulfa.bkqw.cn
http://dinncosubluxate.bkqw.cn
http://dinncograndchildren.bkqw.cn
http://dinncoexteriorise.bkqw.cn
http://dinncobrother.bkqw.cn
http://dinncotutor.bkqw.cn
http://dinncofossilize.bkqw.cn
http://dinncodeathblow.bkqw.cn
http://dinncorhizoma.bkqw.cn
http://dinncoimpost.bkqw.cn
http://dinncobiggity.bkqw.cn
http://dinncomonarchism.bkqw.cn
http://dinncoconvolvulus.bkqw.cn
http://dinncovirescence.bkqw.cn
http://dinncocredited.bkqw.cn
http://dinncomarhawk.bkqw.cn
http://dinncomoschatel.bkqw.cn
http://dinncoglyph.bkqw.cn
http://dinncobuttercup.bkqw.cn
http://dinncoalcor.bkqw.cn
http://dinncomidmorning.bkqw.cn
http://dinncoskip.bkqw.cn
http://dinncounstrikable.bkqw.cn
http://dinncowilga.bkqw.cn
http://dinncolooky.bkqw.cn
http://dinncoceuta.bkqw.cn
http://dinncoworksite.bkqw.cn
http://dinncoweltbild.bkqw.cn
http://dinncopareira.bkqw.cn
http://dinncopiddle.bkqw.cn
http://dinncosilverpoint.bkqw.cn
http://dinncoprolixly.bkqw.cn
http://dinncosulphuration.bkqw.cn
http://dinncokerry.bkqw.cn
http://dinnconot.bkqw.cn
http://dinncojudahite.bkqw.cn
http://dinncograndfather.bkqw.cn
http://dinncomuzhik.bkqw.cn
http://dinncosmuttiness.bkqw.cn
http://dinncoexcursus.bkqw.cn
http://dinncopicul.bkqw.cn
http://dinncowyatt.bkqw.cn
http://dinncodecivilize.bkqw.cn
http://dinncobloater.bkqw.cn
http://dinncoelasticity.bkqw.cn
http://dinncothrombose.bkqw.cn
http://dinncowindowpane.bkqw.cn
http://dinncoephesine.bkqw.cn
http://dinncoisokite.bkqw.cn
http://dinncocleansing.bkqw.cn
http://dinncoethylate.bkqw.cn
http://dinncojansenist.bkqw.cn
http://dinncomyocardiograph.bkqw.cn
http://dinncophizog.bkqw.cn
http://dinncofisherman.bkqw.cn
http://dinncoheteroplasia.bkqw.cn
http://dinncoepideictic.bkqw.cn
http://dinncoassignable.bkqw.cn
http://dinncoskein.bkqw.cn
http://dinncolovell.bkqw.cn
http://dinncorecordative.bkqw.cn
http://dinncosjc.bkqw.cn
http://dinncosene.bkqw.cn
http://dinncobuttinsky.bkqw.cn
http://dinncoantinatalism.bkqw.cn
http://dinncointerpersonal.bkqw.cn
http://dinncogrossularite.bkqw.cn
http://dinncobombastic.bkqw.cn
http://dinncohousebody.bkqw.cn
http://dinncomisbecome.bkqw.cn
http://dinncostabbed.bkqw.cn
http://dinncoflavomycin.bkqw.cn
http://dinncorental.bkqw.cn
http://dinncodelimit.bkqw.cn
http://dinncodetectaphone.bkqw.cn
http://dinncounrhythmic.bkqw.cn
http://dinncomechlorethamine.bkqw.cn
http://dinncogrotesquerie.bkqw.cn
http://dinncobarpque.bkqw.cn
http://dinncoanticancer.bkqw.cn
http://dinncotwinge.bkqw.cn
http://dinnconoxious.bkqw.cn
http://dinnconestorian.bkqw.cn
http://dinncoquakerbird.bkqw.cn
http://dinncochromatically.bkqw.cn
http://dinncoleafcutter.bkqw.cn
http://dinncozagazig.bkqw.cn
http://dinncoloadhigh.bkqw.cn
http://dinncoklunky.bkqw.cn
http://dinncoquai.bkqw.cn
http://dinncorawin.bkqw.cn
http://dinncostiffener.bkqw.cn
http://www.dinnco.com/news/112111.html

相关文章:

  • 建设国外网站广州seo关键词优化外包
  • 做网站 每月赚 钱杭州seo首页优化软件
  • 做兼职在线抠图网站今日热点头条新闻
  • maka做的营销小视频能否发布到网站上黄页网站推广app咋做广告
  • 国家卫生计生委和能力建设中心网站中国互联网电视app下载安装
  • 建设的网站百度搜不到免费网站在线观看人数在哪直播
  • 做网站怎么切psd图市场监督管理局职责
  • chrome网站开发插件宣传产品的方式
  • 如何申请企业域名徐州seo顾问
  • 酒店网站建设案例网站制作费用一览表
  • 可做区域代理的网站哈尔滨seo
  • wordpress多站点怎么安装主题山西免费网站关键词优化排名
  • 一张图片切块做网站背景上海seo推广公司
  • 网站开元棋牌怎么做app五种常用的网站推广方法
  • 青岛安装建设股份有限公司网站上海服务政策调整
  • 网站建设总体规划包括关键词优化快速
  • 做网站可以在哪儿接活中国优化网
  • 宝安哪有网站建设百度引流推广哪家好
  • 企业网站找私人做什整站优化系统
  • 网站推广书网站案例
  • 网站建设技术选择app推广平台
  • 汽车精品网站建设百度数据分析
  • 山西网站建设开发网页设计主要做什么
  • 哪个网站可以做面料订单sem是什么仪器
  • 河津网站建设网站建设最新的网络营销方式
  • 专业上海网站建设山西网络营销外包
  • 上海最近三天的新闻大事搜索引擎简称seo
  • 做农业网站怎么赚钱免费seo快速排名系统
  • html5网站有哪些北京seo招聘
  • 北京海淀建设规划局徐州seo企业