内容简介:要區別 pointer 跟 value 可以透過下面的例子快速了解:
Struct Method 在 Go 語言 開發上是一個很重大的功能,而新手在接觸這塊時,通常會搞混為什麼會在 function 內的 struct name 前面多一個 *
pointer 符號,而有時候又沒有看到呢?以及如何用 struct method 實現 Chain 的實作,本影片會實際用寄信當作範例講解什麼時候該用 pointer
什麼時候該用用 Value
。也可以參考我之前的一篇文章『 Go 語言內 struct methods 該使用 pointer 或 value 傳值? 』
教學影片
範例
要區別 pointer 跟 value 可以透過下面的例子快速了解:
package main import "fmt" type car struct { name string color string } func (c *car) SetName01(s string) { fmt.Printf("SetName01: car address: %p\n", c) c.name = s } func (c car) SetName02(s string) { fmt.Printf("SetName02: car address: %p\n", &c) c.name = s } func main() { toyota := &car{ name: "toyota", color: "white", } fmt.Printf("car address: %p\n", toyota) fmt.Println(toyota.name) toyota.SetName01("foo") fmt.Println(toyota.name) toyota.SetName02("bar") fmt.Println(toyota.name) toyota.SetName02("test") fmt.Println(toyota.name) }
上面範例可以看到如果是透過 SetName02
來設定最後是拿不到設定值,這就代表使用 SetName02
時候,是會將整個 struct 複製一份。假設 struct 內有很多成員,這樣付出的代價就相對提高。
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Heuristic Search
Stefan Edelkamp、Stefan Schrodl / Morgan Kaufmann / 2011-7-15 / USD 89.95
Search has been vital to artificial intelligence from the very beginning as a core technique in problem solving. The authors present a thorough overview of heuristic search with a balance of discussio......一起来看看 《Heuristic Search》 这本书的介绍吧!