简体   繁体   English

如何在 Golang 中正确使用指针

[英]How to use Pointers correctly in Golang

I am new to the go language and am in a scenario where my program works but not sure whether I should have used pointers or not.我是go语言的新手,并且处于我的程序可以工作但不确定我是否应该使用指针的场景中。

Here is my program:这是我的程序:

package main

import (
    "fmt"
    "math"
)

type Shape interface {
    getVolume() float64
}

type Cube struct {
    Side float64
}

type RectanglePrism struct {
    Length float64
    Width  float64
    Height float64
}


func main() {
    c := Cube{Side: 3}
    r := RectanglePrism{Length: 4, Width: 3, Height: 3}
    fmt.Println(Volume(c))
    fmt.Println(Volume(r))

}

func (c Cube) getVolume() float64 {
    return math.Pow(c.Side, 3)
}

func Volume(s Shape) float64 {
    return s.getVolume()
}

func (r RectanglePrism) getVolume() float64 {
    return r.Length * r.Width * r.Height
}

Playground: https://play.golang.org/p/LKSvJXNCEbQ游乐场: https://play.golang.org/p/LKSvJXNCEbQ

As you can see Volume does not modify values in c , just does a calculation and therefore it doesn't necessarily need to point to the original value - nonetheless it does a copy which is more expensive正如您所看到的, Volume不会修改c中的值,只是进行计算,因此它不一定需要指向原始值 - 但是它会进行更昂贵的副本

My questions are:我的问题是:

  • getVolume() should receive a pointer to Cube and RectangularPrism ? getVolume()应该接收一个指向CubeRectangularPrism的指针?
  • Is there a better way of doing this?有没有更好的方法来做到这一点?

Aim here is to right efficient code.这里的目标是正确高效的代码。

Thanks in advance提前致谢

As defined in this https://golang.org/doc/faq#methods_on_values_or_pointers如本https://golang.org/doc/faq#methods_on_values_or_pointers中所定义

if you are modifying data in the struct or handling huge amounts of data or if your struct is very huge its better to use a pointer.如果您正在修改结构中的数据或处理大量数据,或者您的结构非常庞大,则最好使用指针。

as in the above cases, if you are passing it by the value it makes a difference as through each function call you pass a new copy of the struct which might affect your application's efficiency.与上述情况一样,如果您通过值传递它,它会有所不同,因为通过每个 function 调用您传递结构的新副本,这可能会影响您的应用程序的效率。

for very small data passing it by value or pointer won't make a difference as the difference would be negligible(like in your example it won't make a difference) https://play.golang.org/p/t3_Xzhq88g-对于非常小的数据,通过值或指针传递它不会产生影响,因为差异可以忽略不计(就像在你的例子中它不会产生影响) https://play.golang.org/p/t3_Xzhq88g-

if you decide on a way to call function all your other functions will also have to be called in a similar fashion.如果您决定调用 function 的方式,您的所有其他函数也必须以类似的方式调用。

so analyse your use case (these are some suggestive uses cases there might be more scenarios)所以分析你的用例(这些是一些建议性用例,可能会有更多场景)

1) are you gonna modify data in the struct 1)你要修改结构中的数据吗

2) is the data gonna be huge 2)数据会很大吗

3) or if the struct is gonna be huge 3)或者如果结构会很大

4) are you gonna return the modified values (like in your example) 4)你要返回修改后的值吗(就像你的例子一样)

and based you these you can use a pointer or pass-by-value并基于您这些您可以使用指针或按值传递

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM