K346K346
恋喵大鲤鱼
2019-01-13 21:39
采纳率: 43.6%
浏览 1.5k

go为什么不能获取字符串某个字符地址?

go为什么不能获取字符串某个字符地址?go如此设定的目的是什么?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • oyljerry
    oyljerry 2019-01-14 09:53
    已采纳

    这个是go的字符串规定的,因为它是immutable类型,所以让你取某个字符的地址,官方不觉得有啥意义

    A string type represents the set of string values. A string value is a (possibly empty) sequence of bytes. Strings are immutable: once created, it is impossible to change the contents of a string. The predeclared string type is string.

    The length of a string s (its size in bytes) can be discovered using the built-in function len. The length is a compile-time constant if the string is a constant. A string's bytes can be accessed by integer indices 0 through len(s)-1. It is illegal to take the address of such an element; if s[i] is the i'th byte of a string, &s[i] is invalid.

    点赞 评论
  • caozhy

    可以获得某个字符的地址,和C语言类似 &Str + n 就可以了。

    点赞 评论

相关推荐