gostring

package
v0.0.0-...-886b66b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 21, 2022 License: Apache-2.0 Imports: 2 Imported by: 0

README

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()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MyStruct

type MyStruct struct {
	Int64     int64
	StringPtr *string
}

func (*MyStruct) GoString

func (this *MyStruct) GoString() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL