星号在"走"中做了什么

ric*_*h97 46 go

我是一名网络开发人员,希望扩大我的视野,以便在编程方面做得更好.我做了一些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)表示引用.也就是说,创建一个指向变量值或字段的指针.

  • 值得澄清的是,对于 `*v = ...` 情况,指针不会更改为指向内存中具有不同值的新位置,但底层值已更改。 (3认同)

Tom*_*Tom 45

我猜这意味着与C相同

p is a pointer to a string

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

下一行*p = "ciao"会改变内容s

请参阅语言设计常见问题中的此链接

有趣的是,没有指针算术

为什么没有指针算术?安全.如果没有指针算法,就有可能创建一种永远不会导致错误成功的非法地址的语言.编译器和硬件技术已经发展到使用数组索引的循环可以像使用指针算法的循环一样高效的程度.此外,缺少指针算法可以简化垃圾收集器的实现.

现在我想开始学习GO!

  • 学习去吧!我强烈推荐它. (12认同)
  • 当你说*p ="ciao"时,你说的是"把p指向的内存设置为等于"ciao"".如果你说p ="ciao",你会试着说"把p指向地址"ciao","这没有意义,所以它不能编译.p =&s表示"使p指向s的地址".也许这有帮助吗? (11认同)
  • 因此,通过将"p"声明为类型"*string"并将&s指向变量s的内存位置,将"p"创建为指针.那么为什么我只能说p ="ciao"(而不是*p)?这会产生一个名为"p"的新变量吗? (3认同)

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)

  • 说到这里,谢谢你。 (2认同)

Jar*_*Par 5

*字符用于在 C 和 Go 中定义指针。变量不是实际值,而是具有指向值位置的地址。该&运算符用于获取对象的地址。