Skip to content

20190506_what is different between pointer and value in golang

Shawn Wang edited this page Dec 3, 2019 · 1 revision

title: "[Go 語言教學影片] 在 struct 內的 pointers 跟 values 差異" date: 2019-05-06 type: blog author: AppleBoy link: https://blog.wu-boy.com/2019/05/what-is-different-between-pointer-and-value-in-golang/ layout: post comments: true

golang logo

Struct MethodGo 語言開發上是一個很重大的功能,而新手在接觸這塊時,通常會搞混為什麼會在 function 內的 struct name 前面多一個 * pointer 符號,而有時候又沒有看到呢?以及如何用 struct method 實現 Chain 的實作,本影片會實際用寄信當作範例講解什麼時候該用 pointer 什麼時候該用用 Value。也可以參考我之前的一篇文章『Go 語言內 struct methods 該使用 pointer 或 value 傳值?

教學影片

<iframe width="560" height="315" src="https://www.youtube.com/embed/36X8uf7AxOg" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>

範例

要區別 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(&quot;car address: %p\n&quot;, toyota)

fmt.Println(toyota.name)
toyota.SetName01(&quot;foo&quot;)
fmt.Println(toyota.name)
toyota.SetName02(&quot;bar&quot;)
fmt.Println(toyota.name)
toyota.SetName02(&quot;test&quot;)
fmt.Println(toyota.name)

}

上面範例可以看到如果是透過 SetName02 來設定最後是拿不到設定值,這就代表使用 SetName02 時候,是會將整個 struct 複製一份。假設 struct 內有很多成員,這樣付出的代價就相對提高。

Clone this wiki locally