The deriveGoString function returns a string that reproduces the argument's value in valid go syntax.
The deriveGoString function does a recursive print, even printing pointer values, unlike the default %#v operand.
Given the following input:
package gostring
type MyStruct struct {
Int64 int64
StringPtr *string
}
func (this *MyStruct) GoString() string {
return deriveGoString(this)
}
goderive will generate the following code:
// Code generated by goderive DO NOT EDIT.
package gostring
import (
"bytes"
"fmt"
)
// deriveGoString returns a recursive representation of this as a valid go string.
func deriveGoString(this *MyStruct) string {
buf := bytes.NewBuffer(nil)
fmt.Fprintf(buf, "func() *gostring.MyStruct {\n")
if this == nil {
fmt.Fprintf(buf, "return nil\n")
} else {
fmt.Fprintf(buf, "this := &gostring.MyStruct{}\n")
fmt.Fprintf(buf, "this.Int64 = %#v\n", this.Int64)
if this.StringPtr != nil {
fmt.Fprintf(buf, "this.StringPtr = func (v string) *string { return &v }(%#v)\n", *this.StringPtr)
}
fmt.Fprintf(buf, "return this\n")
}
fmt.Fprintf(buf, "}()\n")
return buf.String()
}