Git Product home page Git Product logo

go-render's Introduction

go-render: A verbose recursive Go type-to-string conversion library.

Forked from https://github.com/luci/go-render

This fork adds an "AsCode" function to do everything render.Render previously did, but clean up output to be re-usable as code. The direct render.AsCode string output can be pasted as a new struct without modification and the go linter will be happy.

Overview

The render package implements a more verbose form of the standard Go string formatter, fmt.Sprintf("%#v", value), adding:

  • Pointer recursion. Normally, Go stops at the first pointer and prints its address. The render package will recurse and continue to render pointer values.
  • Recursion loop detection. Recursion is nice, but if a recursion path detects a loop, render will note this and move on.
  • Custom type name rendering.
  • Deterministic key sorting for string- and int-keyed maps.
  • Testing!

Call render.Render and pass it an interface{}.

For example:

type customType int
type testStruct struct {
        S string
        V *map[string]int
        I interface{}
}

a := testStruct{
        S: "hello",
        V: &map[string]int{"foo": 0, "bar": 1},
        I: customType(42),
}

fmt.Println("Render test:")
fmt.Printf("fmt.Printf:    %#v\n", a)))
fmt.Printf("render.Render: %s\n", Render(a))

Yields:

fmt.Printf:    render.testStruct{S:"hello", V:(*map[string]int)(0x600dd065), I:42}
render.Render: render.testStruct{S:"hello", V:(*map[string]int){"bar":1, "foo":0}, I:render.customType(42)}

go-render's People

Contributors

danjacques avatar riannucci avatar gdex-lab avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.