我是一名网络开发人员,希望扩大我的视野,以便在编程方面做得更好.我做了一些Java和一些简单的Android应用程序.我现在正在研究像C和Go这样的低级语言(到目前为止,我必须说它有一些漂亮的语法和很棒的想法,尽管我可能太缺乏经验而无法发表评论).
所以是的,我一直在努力去理解Go网站上的例子,我在这样的例子中一直遇到一个特殊的星号字符:
s := "hello"
if s[1] != 'e' {
os.Exit(1)
}
s = "good bye"
var p *string = &s
*p = "ciao"
Run Code Online (Sandbox Code Playgroud)
另外,我刚注意到,"&s"是什么,它是通过引用分配的(我可能在这里使用PHP会话)?
Mar*_*rot 92
*
附加到type(*string
)表示指向该类型的指针.
*
附加到赋值中的变量(*v = ...
)表示间接赋值.也就是说,更改变量指向的值.
*
附加到变量或表达式(*v
)表示指针取消引用.也就是说,取变量指向的值.
&
附加到变量或表达式(&v
)表示引用.也就是说,创建一个指向变量值或字段的指针.
Tom*_*Tom 45
我猜这意味着与C相同
p is a pointer to a string
该语句var p *string = &s
将分配s
对象的地址p
下一行*p = "ciao"
会改变内容s
请参阅语言设计常见问题中的此链接
有趣的是,没有指针算术
为什么没有指针算术?安全.如果没有指针算法,就有可能创建一种永远不会导致错误成功的非法地址的语言.编译器和硬件技术已经发展到使用数组索引的循环可以像使用指针算法的循环一样高效的程度.此外,缺少指针算法可以简化垃圾收集器的实现.
现在我想开始学习GO!
blo*_*les 25
Go lang 地址,指针和类型:
s := "hello" // type string
t := "bye" // type string
u := 44 // type int
v := [2]int{1, 2} // type array
Run Code Online (Sandbox Code Playgroud)
所有这些Go变量都有一个地址.即使是"指针"类型的变量也有地址.区别是字符串类型保存字符串值,int类型保存整数值,指针类型保存地址.
&
==评估要解决,或者想"这是我的地址,所以你知道在哪里找到我"
// make p type pointer (to string only) and assign value to address of s
var p *string = &s // type *string
// or
q := &s // shorthand, same deal
Run Code Online (Sandbox Code Playgroud)
*
==取消引用指针,或者认为"将操作传递给地址是我的价值"
*p = "ciao" // change s, not p, the value of p remains the address of s
// j := *s // error, s is not a pointer type, no address to redirect action to
// p = "ciao" // error, can't change to type string
p = &t // change p, now points to address of t
//p = &u // error, can't change to type *int
// make r type pointer (to pointer [to string]) and assign value to address of p
var r **string = &p // shorthand: r := &p
w := ( r == &p) // ( r evaluates to address of p) w = true
w = ( *r == p ) // ( *r evaluates to value of p [address of t]) w = true
w = (**r == t ) // (**r evaluates to value of t) w = true
// make n type pointer (to string) and assign value to address of t (deref'd p)
n := &*p
o := *&t // meaningless flip-flop, same as: o := t
// point y to array v
y := &v
z := (*y)[0] // dereference y, get first value of element, assign to z (z == 1)
Run Code Online (Sandbox Code Playgroud)
在这里玩:http://play.golang.org/p/u3sPpYLfz7
M K*_*M K 12
这就是我的看法。不同的措辞可能有助于某人更好地理解它(您可以复制粘贴该代码并检查输出):
package main
import (
"fmt"
)
func main() {
// declare a variable of type "int" with the default value "0"
var y int
// print the value of y "0"
fmt.Println(y)
// print the address of y, something like "0xc42008c0a0"
fmt.Println(&y)
// declare a variable of type "int pointer"
// x may only hold addresses to variables of type "int"
var x *int
// y may not simply be assigned to x, like "x = y", because that
// would raise an error, since x is of type "int pointer",
// but y is of type "int"
// assign address of y "0xc42008c0a0" as value to x
x = &y
// print the value of x "0xc42008c0a0" which is also the address of y
fmt.Println(x)
// print the address of x, something like "0xc420030028"
// (x and y have different addresses, obviously)
fmt.Println(&x)
// x is of type "int pointer" and holds an address to a variable of
// type "int" that holds the value "0", something like x -> y -> 0;
// print the value of y "0" via x (dereference)
fmt.Println(*x)
// change the value of y via x
*x = 1; /* same as */ y = 1
// print value of y "1"
fmt.Println(y); /* same as */ fmt.Println(*x)
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
23512 次 |
最近记录: |