在你的情况下,为什么应该使用指针(Pointer)在 Golang 中?

huangapple go评论78阅读模式
英文:

Why Should use Pointer in my case Golang?

问题

我刚学习了Golang。

在我脑海中,我在想为什么需要指针。如果你只是想将变量a的值赋给变量b,只需使用这个代码:varA := 10, varB := varA(抱歉,我不知道如何将代码换行),我对何时使用指针感到困惑。

英文:

*with a note I just learned golang

in my mind I just think why there should be a pointer, if you want to enter the value of variable a to variable b only with this varA := 10, varB:= varA(sry idk how the code to the nextline) and I'm confused when to use a pointer?

答案1

得分: 4

请按照golang教程进行操作。
https://go.dev/tour/moretypes/1

Go编程语言中的指针(Pointers)是用于存储另一个变量的内存地址的变量。在Golang中,指针也被称为特殊变量。变量用于在系统中的特定内存地址存储一些数据。内存地址始终以十六进制格式表示(以0x开头,如0xFFAAF等)[https://www.geeksforgeeks.org/pointers-in-golang]。
在传递之前复制大型数据类型(如结构体)会花费时间并消耗内存。这也是为什么程序员喜欢在处理大型数据类型时使用指针的另一个原因。

指针的一个示例用法:

package main

import "fmt"

func main() {

    // 声明一个普通变量
    var x int = 5748

    // 声明一个指针
    var p *int

    // 初始化指针
    p = &x

    // 显示结果
    fmt.Println("Value stored in x =", x)
    fmt.Println("Address of x =", &x)
    fmt.Println("Value stored in variable p =", p)
}

输出:

Value stored in x = 5748
Address of x = 0x414020
Value stored in variable p = 0x414020

以下是一些可能对您有用的文章:

英文:

Please follow golang tour.
https://go.dev/tour/moretypes/1

Pointers in Go programming language or Golang is a variable that is used to store the memory address of another variable. Pointers in Golang is also termed as the special variables. The variables are used to store some data at a particular memory address in the system. The memory address is always found in hexadecimal format(starting with 0x like 0xFFAAF etc.)[https://www.geeksforgeeks.org/pointers-in-golang].
Making a copy of large data types like struct before passing would take time and would consume memory. This is another reason why programmers prefer pointers for big data types.

An example use of pointers

package main
 
import "fmt"
 
func main() {
 
    // taking a normal variable
    var x int = 5748
     
    // declaration of pointer
    var p *int
     
    // initialization of pointer
    p = &x
 
        // displaying the result
    fmt.Println("Value stored in x = ", x)
    fmt.Println("Address of x = ", &x)
    fmt.Println("Value stored in variable p = ", p)
}

output:

Value stored in x =  5748
Address of x =  0x414020
Value stored in variable p =  0x414020

Check out the following articles which may be useful for you :

答案2

得分: 2

何时使用值和指针取决于您对数据要执行的操作,并且会影响数据在内存中的存储方式。

如果您计划仅在本地函数中操作数据,请使用值。这将在本地函数本身的上下文中存储数据(称为堆栈),几乎没有垃圾回收开销。

如果您计划将数据传递给另一个函数,并且不希望该函数中的代码影响此函数对数据的视图,请使用值。这样,当您将数据传递到其他任何地方时,您将传递数据的副本。此副本将存在于该函数的本地堆栈中。

如果您计划将数据传递到其他地方,但希望在那里对其进行的更改对此处的代码可见,或者如果数据太大而无法复制,请使用指针。当您拥有指针时,实际上是持有数据的内存地址,而不是数据本身,因此当您在任何地方传递此地址时,所有更改都会影响原始数据 - 数据本身永远不会被复制。这是通过将数据移动到一种称为的全局数据存储系统来实现的,Go只会在确定使用该地址的所有位置不再运行(这个过程称为垃圾回收)后,才会从堆中删除数据。

英文:

When to use a value and pointer depends on what you're going to do with the data, and will affect how it's stored in memory.

If you plan to operate on the data only in your local function, use the value. The stores the data in the context of the local function itself (called the stack) and has almost no garbage collection overhead.

If you plan to pass the data elsewhere to another function, and you do not want the code in that function to affect this function's view of the data, use the value. That way, when you pass the data anywhere else, you'll be passing a copy of the data. This copy will exist on that function's local stack.

If you plan to pass the data elsewhere but want the changes that happen to it there to be visible to your code here, or if you data is too large to copy, use a pointer. When you have a pointer you're actually holding the memory address of the data instead of the data itself, so when you pass this address around all changes made anywhere affect the original data - the data itself is never copied. This is done by moving the data to a kind of global data storage system called the heap, and Go will delete the data off the heap only after it determines that all the places using that address are no longer functioning (the process called garbage collection).

huangapple
  • 本文由 发表于 2022年8月7日 14:25:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/73265341.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定