英文:
Take the address of a character in string
问题
package main
import (
"fmt"
)
func main(){
var str string = "hello,world"
fmt.Println(&str)
fmt.Println(&str[0])
}
我是一个完全的Go语言新手。刚开始学习几天。这是我的问题:
编译错误:无法获取str[0]的地址
我在《The Way to Go》中搜索了一下。它说你不能获取字符串中字符的地址。为什么不允许这样做?
另一个困惑是,一旦创建了一个字符串,就不能再修改它了。这是否意味着字符串在常量空间中?
英文:
package main
import (
"fmt"
)
func main(){
var str string = "hello,world"
fmt.Println(&str)
fmt.Println(&str[0])
}
I'm a complete rookie in golang. Just started learning for a few
days. Here is my problem:
> compiled error: cannot take the address of str[0]
I searched <<the way to go>>. It says you cannot take the
address of a character in a string. Why is it not allowed?
Another confusion is that once you create a string, you
cannot modify it anymore. Does that mean the string is in constant
space?
答案1
得分: 4
是的,在Go文档中可以看到这个解释:
字符串类型表示一组字符串值。字符串值是一个(可能为空)字节序列。字符串是不可变的:一旦创建,就无法更改字符串的内容。预声明的字符串类型是string。
可以使用内置函数len来获取字符串s的长度(以字节为单位)。如果字符串是常量,则长度是编译时常量。可以通过整数索引0到len(s)-1来访问字符串的字节。对这样的元素取地址是非法的;如果s[i]是字符串的第i个字节,则&s[i]是无效的。
希望这能解决你的疑问。
英文:
Yes this is correct in Go Documentation you can see that
> > 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.
I hope this will solve your doubts
答案2
得分: 1
为了澄清对未来访问者有关此问题的答案,访问字符串的第i个字节的地址,例如&s[i]
是无效的,因为字符串是不可变的。如果可以获取地址,那么字符串就可以被改变。
相同的逻辑也适用于字面字符串。
s := "stack overflow"
fmt.Println(&s) // 正常工作
fmt.Println(&"hello, world") // 运行时错误
这可能看起来很奇怪,但如果你将其视为常量,它就更有意义。为了保持一致性,人们也应该允许获取其他常量的地址,比如&42
或&true
。
英文:
To clarify the answer for future visitors to this question, accessing the address of the i-th byte of a string like &s[i]
is invalid because strings are immutable. If you could get the address, then the string can be mutated.
The same logic applies to literal strings.
s := "stack overflow"
fmt.Println(&s) // works
fmt.Println(&"hello, world") // runtime error
This may seem strange, but it makes more sense if you think of it as a constant. For consistency, one would then have to allow taking the address of other constants, like &42
or &true
.
答案3
得分: 0
内置类型字符串,其底层结构是reflect.StringHeader。
reflect.StringHeader.Data(uinptr)是字符串内容的内存地址。
尝试这样做:
(*reflect.StringHeader)(unsafe.Pointer(strPtr)).Data
英文:
builtin type string, its underlying struct is reflect.StringHeader
reflect.StringHeader.Data(uinptr), is string content memory address
try this:
(*reflect.StringHeader)(unsafe.Pointer(strPtr)).Data
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论