2018-08-20から1日間の記事一覧

deferの中でmutationな操作をする関数を読んで戻り値を変更しても、戻り値に影響がないのは何故だろう

package main import "fmt" type Test struct { value int } func One(test *Test) { test.value = 1 fmt.Println("one") } func execute() Test { x := Test{0} defer One(&x) // xのポインタをOne関数に渡して、0を1に更新 return x } func main() { x := …

A Tour of Goをやるぞ(Methods and interfaces)

これをやる A Tour of Go 型にメソッドを定義 funcと関数名の間にreceiverを定義することで型にメソッドを定義をできる。 メソッドはreceiverを持った関数のことである。 package main import ( "fmt" ) func (test Test) Double() int { return test.X * te…