GoのOO


テストまで24時間切ったというのにGo言語ばかりいじっている。これは間違いなくてテスト終わったら飽きるパターン... !!
今のところGoのイメージは C にpythonjavascriptの文法を適当に載っけた感じ

type InternalObj struct { a,b int}
type Obj struct {
	title string
	internal InternalObj
}
func (o Obj) produce() { fmt.Println( "My name is "+ o.title) }
func (o *Obj) set(s string) { o.title = s }

func main(){
	obj := new(Obj)
	obj.set("Mizchi")
	obj.produce()
	println((obj.internal.a +3))
}

一応入れ子にしたりできる
コンストラクタはどう設定したらいいだろう。



インターフェースを使うとこんな感じになる
Java等にあまりなじんでいないのでinterfaceの使い方がよくわからない

package main
import "fmt"

//interface
type AllowedMethods interface{
	produce()
	set(s string)
}

func produce(inf AllowedMethods){ inf.produce() }
func set(inf AllowedMethods,s string){ inf.set(s) }

//structure
type InternalObj struct { a,b int}
type Obj struct {
	title string
	internal InternalObj
}

func (o Obj) produce() { fmt.Println( "My name is "+ o.title) }
func (o *Obj) set(s string) { o.title = s }
func (o *Obj) init() { o.title = "Hoge" }

func main() {
	obj := new(Obj)
	obj.init()
	
	// obj.set("Mizchi")
	obj.produce()
	println((obj.internal.a +3))
	
	// interfaceを介したアクセス
	set(obj,"bar")
	produce(obj)
}