• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

.travis.ymlH A D18-Aug-201861

COPYINGH A D18-Aug-20181.1 KiB

README.mdH A D18-Aug-20182.1 KiB

repr.goH A D18-Aug-20187.8 KiB

repr_test.goH A D18-Aug-20181.8 KiB

README.md

1# Python's repr() for Go [![](https://godoc.org/github.com/alecthomas/repr?status.svg)](http://godoc.org/github.com/alecthomas/repr) [![Build Status](https://travis-ci.org/alecthomas/repr.png)](https://travis-ci.org/alecthomas/repr)
2
3This package attempts to represent Go values in a form that can be used almost directly in Go source
4code.
5
6Unfortunately, some values (such as pointers to basic types) can not be represented directly in Go.
7These values will be represented as `&<value>`. eg. `&23`
8
9## Example
10
11```go
12type test struct {
13  S string
14  I int
15  A []int
16}
17
18func main() {
19  repr.Print(&test{
20    S: "String",
21    I: 123,
22    A: []int{1, 2, 3},
23  })
24}
25```
26
27Outputs
28
29```
30&main.test{S: "String", I: 123, A: []int{1, 2, 3}}
31```
32
33## Why repr and not [pp](https://github.com/k0kubun/pp)?
34
35pp is designed for printing coloured output to consoles, with (seemingly?) no way to disable this. If you don't want coloured output (eg. for use in diffs, logs, etc.) repr is for you.
36
37## Why repr and not [go-spew](https://github.com/davecgh/go-spew)?
38
39Repr deliberately contains much less metadata about values. It is designed to (generally) be copyable directly into source code.
40
41Compare go-spew:
42
43```go
44(parser.expression) (len=1 cap=1) {
45 (parser.alternative) (len=1 cap=1) {
46  ([]interface {}) (len=1 cap=1) {
47   (*parser.repitition)(0xc82000b220)({
48    expression: (parser.expression) (len=2 cap=2) {
49     (parser.alternative) (len=1 cap=1) {
50      ([]interface {}) (len=1 cap=1) {
51       (parser.str) (len=1) "a"
52      }
53     },
54     (parser.alternative) (len=1 cap=1) {
55      ([]interface {}) (len=1 cap=1) {
56       (*parser.self)(0x593ef0)({
57       })
58      }
59     }
60    }
61   })
62  }
63 }
64}
65```
66
67To repr:
68
69```go
70parser.expression{
71  parser.alternative{
72    []interface {}{
73      &parser.repitition{
74        expression: parser.expression{
75          parser.alternative{
76            []interface {}{
77              parser.str("a"),
78            },
79          },
80          parser.alternative{
81            []interface {}{
82              &parser.self{              },
83            },
84          },
85        },
86      },
87    },
88  },
89}
90```
91