星号在“Go”中做了什么? [英] What does the asterisk do in "Go"

查看:365
本文介绍了星号在“Go”中做了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名Web开发人员,希望扩展自己的视野,以便更好地整体编程。我已经做了一些Java和一些简单的Android应用程序。我现在正在研究像C和Go这样的低级语言(到目前为止,我必须说有一些漂亮的语法和很棒的想法,尽管我可能没有经验来评论)。

所以,我一直在努力理解 Go网站和我一直在遇到一个特殊的星号字符,例如像这样:

  s:=hello
if s [1]!='e'{
os.Exit(1)
}
s =good bye
var p * string =& s
* p =ciao

另外,我刚注意到,& s它通过引用分配(我可能在这里使用PHP的谈话)?

解决方案

我猜这意味着与C中相同。 code> p是一个指向字符串的指针



语句 var p * string =& s 会将 s 对象的地址分配给 p



下一行 * p =ciao会改变 s p>

请参阅语言设计常见问题解答

有趣的是,没有指针运算为什么没有指针运算?
安全。如果没有指针运算
,可以创建一个语言
,永远不会导致错误地成功的非法
地址。
编译器和硬件技术具有
的先进性,使用数组索引的循环
可以像使用指针
算术的循环一样高效
。另外,缺少指针
算术可以简化垃圾
收集器的
实现。


现在我想开始学习GO!


I'm a web developer looking to expand my horizons in order to get better at programming as a whole. I've done a bit Java and some simple Android applications. I'm now looking into lower level languages like C and Go (which I must say has some beautiful syntax and great ideas thus far, though I'm maybe too inexperienced to comment).

So yeah I've been going though and trying to understand the examples on the Go website and I keep coming across a special asterisk character in example like this:

s := "hello"
if s[1] != 'e' {
    os.Exit(1)
}
s = "good bye"
var p *string = &s
*p = "ciao"

Also, I just noticed, whats with the "&s" is it assignment by reference (I might be using PHP talk here)?

解决方案

Im guessing it means the same as in C

p is a pointer to a string

The statement var p *string = &s would assign the address of the s object to p

Next line *p = "ciao" would change the contents of s

See this link from the Language Design FAQ

Interestingly, no pointer arithmetic

Why is there no pointer arithmetic? Safety. Without pointer arithmetic it's possible to create a language that can never derive an illegal address that succeeds incorrectly. Compiler and hardware technology have advanced to the point where a loop using array indices can be as efficient as a loop using pointer arithmetic. Also, the lack of pointer arithmetic can simplify the implementation of the garbage collector.

Now I want to start learning GO!

这篇关于星号在“Go”中做了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆