这种“模式”背后的动机是什么? [英] What is the motivation behind this "pattern"?

查看:127
本文介绍了这种“模式”背后的动机是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  bigBox:=& BigBox {} $ b当我看到以下代码时,我有点困惑: $ b bigBox.BubbleGumsCount = 4 //正确... 
bigBox.SmallBox.AnyMagicItem = true //也正确

为什么或者什么时候,我想要做 bigBox:=& BigBox {} 而不是 bigBox:= BigBox {} ?在某种程度上它更有效率吗?代码示例取自 > here



样品2:

  package main 

importfmt

type Ints struct {
x int
y int
}

func build_struct()Ints {
return Ints {0,0}
}
$ b func build_pstruct()* Ints {
return& Ints {0,0 }


func main(){
fmt.Println(build_struct())
fmt.Println(build_pstruct())
}

样品编号3:(为什么我会在这个例子中使用& BigBox,而不是直接使用BigBox作为结构?)

  func main(){
bigBox:=&BigBox {}
bigBox.BubbleGumsCount = 4
fmt.Println(bigBox.BubbleGumsCount)
}

有没有理由调用build_pstruct而不是build_struct变体?这不就是为什么我们有GC吗?

解决方案

我想出了这种代码的一个动机:避免struct复制意外。






如果您使用结构变量来保存新创建的结构:

  bigBox:= BigBox {} 



  myBox:= bigBox //你只需要一个bigBox的refence 。 
myBox.BubbleGumsCount = 4

或者像这样

  changeBoxColorToRed(bigBox)

code> changeBoxColorToRed 是

  //它将整个结构的副本作为参数。 
func changeBoxColorToRed(box bigBox){
// !!!!这个功能是越野车。它不会按预期工作!
//请看最后的修正。
box.Color = red
}






但是如果你使用一个结构体指针:

pre $ bigbox:=& BigBox {}

code>

不会复制

  myBox:= bigBox 

  changeBoxColorToRed(bigBox)

将无法编译,你有机会重新考虑 changeBoxColorToRed 的设计。修复很明显:

  func changeBoxColorToRed(box * bigBox){
box.Color = red
}

新版本的 changeBoxColorToRed 不会复制整个结构并正常工作。


I'm a bit confused when I see code such as:

bigBox := &BigBox{}
bigBox.BubbleGumsCount = 4          // correct...
bigBox.SmallBox.AnyMagicItem = true // also correct

Why, or when, would I want to do bigBox := &BigBox{} instead of bigBox := BigBox{} ? Is it more efficient in some way?

Code sample was taken from here.

Sample no.2:

package main

import "fmt"

type Ints struct {
  x int
  y int
}

func build_struct() Ints {
  return Ints{0,0}
}

func build_pstruct() *Ints {
  return &Ints{0,0}
}

func main() {
  fmt.Println(build_struct())
  fmt.Println(build_pstruct())
}

Sample no. 3: ( why would I go with &BigBox in this example, and not with BigBox as a struct directly ? )

func main() {
  bigBox := &BigBox{}
  bigBox.BubbleGumsCount = 4 
  fmt.Println(bigBox.BubbleGumsCount)
}

Is there ever a reason to call build_pstruct instead of the the build_struct variant? Isn't that why we have the GC?

解决方案

I figured out one motivation for this kind of code: avoidance of "struct copying by accident".


If you use a struct variable to hold the newly created struct:

bigBox := BigBox{}

you may copy the struct by accident like this

myBox := bigBox // Where you just want a refence of bigBox.
myBox.BubbleGumsCount = 4

or like this

changeBoxColorToRed(bigBox)

where changeBoxColorToRed is

// It makes a copy of entire struct as parameter. 
func changeBoxColorToRed(box bigBox){
    // !!!! This function is buggy. It won't work as expected !!!
    // Please see the fix at the end.
    box.Color=red
}


But if you use a struct pointer:

bigBox := &BigBox{}

there will be no copying in

myBox := bigBox

and

changeBoxColorToRed(bigBox)

will fail to compile, giving you a chance to rethink the design of changeBoxColorToRed. The fix is obvious:

func changeBoxColorToRed(box *bigBox){
    box.Color=red
}

The new version of changeBoxColorToRed does not copy the entire struct and works correctly.

这篇关于这种“模式”背后的动机是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆