`*[]types.a` 和 `[]*types.b` 在 Golang 中有什么区别?

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

what is difference between *[] *types.a and [] *types.b in golang

问题

在上述代码中,函数声明如下:

func (c *Congress) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, txs *[]*types.Transaction, uncles []*types.Header, receipts *[]*types.Receipt, systemTxs []*types.Transaction) error {

其中,txs *[]*types.Transactionuncles []*types.Header 是两个参数。在Go语言中,这两个参数的含义如下:

  1. txs *[]*types.Transaction:这是一个指向 []*types.Transaction 类型的指针。[]*types.Transaction 表示一个指向 types.Transaction 类型的切片。因此,txs 是一个指向 types.Transaction 类型切片的指针。

  2. uncles []*types.Header:这是一个 []*types.Header 类型的切片。[]*types.Header 表示一个指向 types.Header 类型的切片。因此,uncles 是一个 types.Header 类型切片。

希望以上解释对你理解Go语言中的术语有所帮助。

英文:

while looking into code I found below function declaration

func (c *Congress) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, txs *[]*types.Transaction, uncles []*types.Header, receipts *[]*types.Receipt, systemTxs []*types.Transaction) error {

where two parameters are txs *[]*types.Transaction, uncles []*types.Header, what does it mean in terms of golang

just want to understand golang terminology with reference to above code

答案1

得分: 0

uncles []*types.Header

uncles是一个切片。如果你将它复制到其他地方(例如通过将其作为函数参数传递),对它的更新不会向原始切片传播(有一个细微差别,即对元素的更改会传播,但原始切片的大小/数据指针将保持不变)。

txs *[]*types.Transaction

txs是一个指向切片的指针。它有点像一个“引用”,因为对所指向的切片的更新会传播到指向该切片的其他地方。因此,如果将一个指向切片的指针作为函数参数传递,函数可能会更改所指向的切片的属性(添加/删除元素)。

英文:

uncles []*types.Header

uncles is slice. If you copy it somewhere (perhaps by passing it in as a function parameter) updates to it will not propagate backwards to the original (with the nuance that changes to elements will propagate, but the size/data pointer of the original will remain unchanged).

txs *[]*types.Transaction

txs is a pointer to a slice. It’s sort of like a “reference” in that updates to the slice pointed at propagate to anyone else pointing to that slice. So if you pass a pointer to a slice as a function parameter, the function may change the properties of the slice pointed to (adding/removing elements).

huangapple
  • 本文由 发表于 2022年10月30日 19:30:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/74252664.html
匿名

发表评论

匿名网友

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

确定