星号在“Go”中起什么作用?

发布于 2024-09-15 08:08:56 字数 311 浏览 7 评论 0原文

我一直在浏览并尝试理解 Go 网站 上的示例,并且不断遇到像这样的例子中的特殊星号字符:

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

另外,我刚刚注意到,&s 是什么?是通过引用赋值吗(我可能在这里使用 PHP 谈话)?

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

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

Also, I just noticed, what's with the &s? Is it assignment by reference (I might be using PHP talk here)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

×眷恋的温暖 2024-09-22 08:08:56

附加到类型 (*string) 的 * 表示指向该类型的指针。

在赋值中附加到变量的 * (*v = ...) 表示间接赋值。即改变变量指向的值。

附加到变量或表达式 (*v) 的 * 表示指针取消引用。也就是说,获取变量指向的值。

附加到变量或表达式 (&v) 的 & 表示引用。也就是说,创建一个指向变量值或字段的指针。

* attached to a type (*string) indicates a pointer to the type.

* attached to a variable in an assignment (*v = ...) indicates an indirect assignment. That is, change the value pointed at by the variable.

* attached to a variable or expression (*v) indicates a pointer dereference. That is, take the value the variable is pointing at.

& attached to a variable or expression (&v) indicates a reference. That is, create a pointer to the value of the variable or to the field.

浅浅淡淡 2024-09-22 08:08:56

我猜这与 C 中的意思相同

p 是指向字符串的指针

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

下一行 *p = "ciao" 将更改 s 的内容

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

有趣的是,没有指针算术

为什么没有指针运算?
安全。没有指针算术
创建一种语言是可能的
永远不可能衍生出非法的
错误成功的地址。
编译器和硬件技术有
前进到循环的地步
使用数组索引可以是
使用指针的循环效率很高
算术。另外,缺少指针
算术可以简化
落实垃圾分类
收集器。

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.

万人眼中万个我 2024-09-22 08:08:56

Go lang 地址、指针和类型:

s := "hello"      // type string
t := "bye"        // type string
u := 44           // type int
v := [2]int{1, 2} // type array
q := &s           // type pointer

所有这些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

在指针变量之前添加 * 可取消对指向其所保存地址的指针的引用。或者想想,“将操作传递到我的值的地址。”

*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 a pointer which is a pointer to a 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)

在这里玩:http://play.golang.org/p/u3sPpYLfz7

Go lang Addresses, Pointers and Types:

s := "hello"      // type string
t := "bye"        // type string
u := 44           // type int
v := [2]int{1, 2} // type array
q := &s           // type pointer

All these Go variables have an address in memory. The distinction between them is string types hold string values, int types hold integer values, and pointer types hold addresses. So even variables that hold addresses have their own memory address.

Pointers:

Adding & before a variable name evaluates to it's address. Or think, "here's my address so you know where to find me."

// 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

Adding * before a pointer variable dereferences the pointer to the address it is holding. Or think, "pass the action on to the address which is my value."

*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 a pointer which is a pointer to a 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)

Go Play here: http://play.golang.org/p/u3sPpYLfz7

暗地喜欢 2024-09-22 08:08:56

我就是这么看的。不同的措辞可能有助于某人更好地理解它(您可以复制粘贴该代码并检查输出):

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)
}

That's how I see it. Different phrasing might help someone to understand it better (you can copy paste that code and examine the output):

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)
}
小糖芽 2024-09-22 08:08:56

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

The * character is used to define a pointer in both C and Go. Instead of a real value the variable instead has an address to the location of a value. The & operator is used to take the address of an object.

携余温的黄昏 2024-09-22 08:08:56

我不懂Go,但根据语法,它似乎类似于C - 那是一个指针。它与参考类似,但级别较低且功能更强大。它包含相关项目的内存地址。 &a 获取变量的内存地址,而 *a 取消引用它,获取内存地址处的值。

另外,声明中的 * 表示它是一个指针。

所以,是的,就像在 PHP 中一样,s 的值发生了变化,因为 p&s 指向同一块内存。

I don't know Go, but based on the syntax, it seems that its similar to C - That is a pointer. Its similar to a reference, but lower level and more powerful. It contains the memory address of the item in question. &a gets the memory address of a variable and *a dereferences it, getting the value at the memory address.

Also, the * in the declaration means that it is a pointer.

So yes, its like in PHP in that the value of s is changed because p and &s point to the same block of memory.

过气美图社 2024-09-22 08:08:56

golang中的*作为指针。指针保存一个值的内存地址。

*T 类型是指向 T 值的指针。它的零值为零。

var p *int

的&运算符生成一个指向其操作数的指针。

i := 42
p = &i

* 运算符表示指针的基础值。

fmt.Println(*p) // read i through the pointer p
*p = 21         // set i through the pointer p

这称为“解除引用”或“引导”。

与 C 不同,Go 没有指针运算。

package main

import "fmt"

func main() {
    i, j := 42, 2701

    p := &i         // point to i
    fmt.Println(*p) // read i through the pointer
    *p = 21         // set i through the pointer
    fmt.Println(i)  // see the new value of i

    p = &j         // point to j
    *p = *p / 37   // divide j through the pointer
    fmt.Println(j) // see the new value of j
}

The * in golang as pointers. A pointer holds the memory address of a value.

The type *T is a pointer to a T value. It's zero value is nil.

var p *int

The & operator generates a pointer to its operand.

i := 42
p = &i

The * operator denotes the pointer's underlying value.

fmt.Println(*p) // read i through the pointer p
*p = 21         // set i through the pointer p

This is known as "dereferencing" or "in directing".

Unlike C, Go has no pointer arithmetic.

package main

import "fmt"

func main() {
    i, j := 42, 2701

    p := &i         // point to i
    fmt.Println(*p) // read i through the pointer
    *p = 21         // set i through the pointer
    fmt.Println(i)  // see the new value of i

    p = &j         // point to j
    *p = *p / 37   // divide j through the pointer
    fmt.Println(j) // see the new value of j
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文