Bye Bye Moore

PoCソルジャーな零細事業主が作業メモを残すブログ

カプセル化とアレコレ

package main

import (
	"fmt"
)

type MyType struct {
	num  int
	body []byte
	ans  string
}

func (mt *MyType) String() string {
	return fmt.Sprintf("%d::%s\n", mt.num, mt.body)
}

func (mt *MyType) setAns() string {
	mt.ans = fmt.Sprintf("Num value is %d. Body value is %s.\n", mt.num, mt.body)
	return mt.ans
}

func main() {
	p := new(MyType)
	p.num = 12
	p.body = []byte("yes")
	fmt.Printf("%v\n", p.String())

	p.setAns()
	fmt.Printf("%v\n", p.ans)
}
$ go run hoge.go 
12::yes

Num value is 12. Body value is yes.