go指南:映射练习

Wesley13
• 阅读 300

题目

题目地址https://tour.go-zh.org/moretypes/23

练习:映射

实现 WordCount。它应当返回一个映射,其中包含字符串 s 中每个“单词”的个数。函数 wc.Test 会对此函数执行一系列测试用例,并输出成功还是失败。

你会发现 strings.Fields 很有帮助。

答案

package main

import (
    "golang.org/x/tour/wc"
    "strings"
)

func WordCount(s string) map[string]int {
    sFields := strings.Fields(s)
    var resMap = map[string]int{}
    for _,v := range sFields{
        resMap[v] ++
    }
    return resMap
}

func main() {
    wc.Test(WordCount)
}

分析

String.Fields

按我的理解就是 将一个字符串,以空格为分隔符,分割为数组。

思路

然后将传过来的字符串拆分为数组后,将对应的Map对应key的值进行+1操作,这里的技巧是没有初始化的key,默认值为0.

降低IDE的Unused错误等级

话说用intellij的IDE goland,会经常报Unused错误,就是说没用使用,但问题我只是还没写完,然后因为是初学golang,以为是什么了不起的错误,就很浪费时间。
如图可以将其改为Warning,Ctrl+Alt+S 打开设置

IDE配置

还可以用Ctrl+Shift+A,进行搜索,如图

image.png

本文同步分享在 博客“xuing”(JianShu)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

点赞
收藏
评论区
推荐文章
Easter79 Easter79
2年前
Vue 全宇宙最浪 VSCode 配置、插件
image别人的那一堆配置、插件我就不写了~首先进入官网下载(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Flinks.jianshu.com%2Fgo%3Fto%3Dhttps%253A%252F%252Fcode.visualstudio.com%252F)并安装。基本
Stella981 Stella981
2年前
Android WebSocket实现即时通讯功能
JavaWebSocket框架GitHub地址:https://github.com/TooTallNate/JavaWebSocket(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Flinks.jianshu.com%2Fgo%3Fto%3Dhttps%253A%
Wesley13 Wesley13
2年前
P2P技术详解(三):P2P中的NAT穿越(打洞)方案详解(进阶分析篇)
1、引言接本系列的上一篇《P2P技术详解(二):P2P中的NAT穿越(打洞)方案详解(基本原理篇)(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Flinks.jianshu.com%2Fgo%3Fto%3Dhttp%253A%252F%252Fwww.52im
Stella981 Stella981
2年前
Aliyun Serverless VSCode Extension v1.10.0 发布
AliyunServerlessVSCodeExtension(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fyq.aliyun.com%2Fgo%2FarticleRenderRedirect%3Furl%3Dhttps%253A%252F%252Fgithub.com%252
Stella981 Stella981
2年前
Nano vs Emacs vs Vim 让你了解他们的不同
Nano(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Flinks.jianshu.com%2Fgo%3Fto%3Dhttp%253A%252F%252Fnanoeditor.org)vsEmacs(https://www.oschina.net/action/GoToLin
Wesley13 Wesley13
2年前
TCP是可靠传输,应用层是否还需要做校验
为建立中文知识库加块砖       ——中科大胡不归0\.原标题:TCP新手误区数据校验的意义(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Flinks.jianshu.com%2Fgo%3Fto%3Dhttps%253A%252F%252Fblog.csdn.
可莉 可莉
2年前
15 分钟部署一个 CAS 服务并对接 Shibboleth
前言这是一个标题党。CAS是一个经典的单点登录方案,又有开源版本(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Flinks.jianshu.com%2Fgo%3Fto%3Dhttps%253A%252F%252Fapereo.github.io%252Fcas%25
Stella981 Stella981
2年前
Extm MyBatis增强工具
Extm是一个MyBatis(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Flinks.jianshu.com%2Fgo%3Fto%3Dhttp%253A%252F%252Fwww.mybatis.org%252Fmybatis3%252F)的增强工具,在MyBatis的
Stella981 Stella981
2年前
Serverless 解惑——函数计算如何访问 MySQL 数据库
函数计算(FunctionCompute):函数计算(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fyq.aliyun.com%2Fgo%2FarticleRenderRedirect%3Furl%3Dhttps%253A%252F%252Fstatistics.functi
Wesley13 Wesley13
2年前
Java 基础(6)——关键字 & 标识符 & 转义字符
依然是基础的一天,看一看就好~关键字之前就有说过关键字哦~注:关键字(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Flinks.jianshu.com%2Fgo%3Fto%3Dhttps%253A%252F%252Fbaike.baidu.com%252Fite