メソッドチェーンは使えないことに気付いた(誤)
*コメントにあるように、メソッドチェーンは使えますが、以下のような現象が起こるのは事実です
しばらく書いてようやく気づきました。仕様はどうか知りませんが、実装としてGo言語はメソッドチェーンは使えないようです。
package main import "fmt" type A struct { i int } func NewA(i int) *A{ return &A{i} } type mystring string func (s *mystring) Len() int { return len(*s) } func (a *A) MyString() mystring { return mystring(fmt.Sprintf("A:%d", a.i)) } func main() { /* //こっちだと動く str := NewA(3).MyString() fmt.Printf("%d\n", str.Len()) */ fmt.Printf("%d\n", NewA(3).MyString().Len()) }
go buildすると、怒られます。
# command-line-arguments ./chain.go:21: cannot call pointer method on NewA(3).MyString() ./chain.go:21: cannot take the address of NewA(3).MyString()