drq22639 2017-06-15 15:43
浏览 8
已采纳

在字符串的索引处获取字符

func lengthOfLongestSubstring(s string) {
    match := make(map[string]int)
    var current string

    current = s[0]

    if match[current] == 1 {
        ///
    }

}

Why do I get the error cannot use s[0] (type byte) as type string in assignment? As far as I can tell it's clear that s is of type string, why does accessing a character turn it into type byte?

  • 写回答

2条回答 默认 最新

  • duan19850312 2017-06-16 03:14
    关注

    Let's read how one of the Go designers look at strings in go:

    In Go, a string is in effect a read-only slice of bytes. If you're at all uncertain about what a slice of bytes is or how it works, please read the previous blog post; we'll assume here that you have.

    It's important to state right up front that a string holds arbitrary bytes. It is not required to hold Unicode text, UTF-8 text, or any other predefined format. As far as the content of a string is concerned, it is exactly equivalent to a slice of bytes.

    So, apparently in your case, s[0] is a byte type, you need explicit case if you really need the assignment.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度