k0kubun / pp Goto Github PK
View Code? Open in Web Editor NEWColored pretty printer for Go language
Home Page: https://pkg.go.dev/github.com/k0kubun/pp/v3
License: MIT License
Colored pretty printer for Go language
Home Page: https://pkg.go.dev/github.com/k0kubun/pp/v3
License: MIT License
I wish there is an option / set of methods that will print maps with keys sorted in a best-effort manner. This can be extremely helpful in diffing outputs across runs.
(As you likely know, spew has a SortKeys
global config that achieves exactly this.)
Thank you for your consideration.
http://blog.golang.org/go-and-google-app-engine
in google appengine/go environment.
unsafe package and syscall package are trimmed.
please don't use that packages.
if an object has a type generated by protobuf, then the pp.Print() will show very very very long content on screen.
It would be better to show the user-defined fields.
type Obj struct {
priv someotherstruct
Pub string
}
var obj Obj
pp.Print(obj)
It prints at present all the priv
also. How to ignore printing private parts of struct
Colors are great and make the output very readable, however it's a pain when trying to output to a file
go-isatty[1] seems like it would be a good addition to this project to solve this
cause stuff like
&big.Int{
neg: false,
abs: big.nat{
0x908f8474ea971baf,
},
}
does look too messy to be honest. Also, some default cool things like http response, rsa keys, etc. could be looking more cool (only most popular, much of default types should be printed explicitly)
Currently, both keys and values for map types are output using the color scheme setting for "String". It would likely make a bit more sense to have field names be colorized using the "FieldName" setting.
When print the byte slice, pp print as hex, quite hard to see.
E.g:
pp.Println("65:", []byte{65})
will print:
print:
"65:" []uint8{
0x41,
}
Expected something like:
"65:" []uint8{
65,
}
Is this possible and useful?
hi,
I started to use this nice lib.
It would be nice to have a function :
func (pp *PrettyPrinter) SetMaxDepth(depth int) {
pp.maxDepth = depth
}
so that we can build different printers with different depths.
It would be nice too to have an option to force the representation of some kinds of variables, like force []byte to string.
or even time.Time to string instead of going deeper ?
cheers
jf
Passing any variable to pp.Printf
under formatting %T
always results in string
type, even when the actual type is different (for example, int
).
package main
import (
"fmt"
"github.com/k0kubun/pp"
)
func main() {
a := 1
fmt.Printf("%T\n", a)
pp.Printf("%T\n", a)
}
int
string
int
int
Go version - 1.17.1
Package (pp) version - 3.0.7 (github.com/k0kubun/pp/v3
)
Vseion: v1.3.1-0.20200204103551-99835366d1cc
Code:
func main(){
pp.WithLineInfo = true
pp.Println("Hello")
}
Output:
github.com/k0kubun/[email protected]/pp.go:176
Hello
Expect:
/Users/linzhanwei/portal-room/main.go:3
Hello
runtime.Caller(2)
should be 3
ps. Why the newest version prefix is v1.3.1? Do I import it wrong?
p := map[string]any{"name": 10, "age": 33}
pp.Printf("address: %p\n", &p)
/* print:
%!p(string=&map[string]interface {}{
"age": 33,
"name": 10,
})
*/
fmt.Printf("address: %p\n", &p)
// print: 0xc00008a9a8
printTime
assumes the value is from the go standard library. This is not always the case as some libraries redefine the time.Time
type in the same package (i.e. Helm https://github.com/helm/helm/blob/master/pkg/time/time.go)
func main() {
pp.Printf("%d", 123)
fmt.Println()
}
The code outputs
%!d(string=123)
I'm using the latest version: v3.1.1-0.20220216035903-af1e26e8087e
Great prettyprinter! However, it would be nice to provide the option to not print struct metadata (like JSON strings.)
I want to print a struct contains a large buffer.
type Foo struct {
B [65535]byte
}
func main() {
m := Foo{}
pp.Print(m)
}
But it's not pretty and takes a long time:
...
...
...
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
...
...
...
I prefer large buffers printed like this:
main.Foo{
B: [65535]uint8{...},
}
For sparse data structures omission of uninitialized members by default would help with readability.
It would be nice to have a simple way to disable all color (e.g., a function call, or option for New()
. The standard way is working fine, though; it's just a nuisance.
I want to print numbers as decimal, but now it's hex format. I see the mater now supports a flag p.decimalUint
, but my installed version is: github.com/k0kubun/pp v1.3.1-0.20200505181502-f5c257df338e
, which doesn't contain this flag, it just prints with "%#v"
I installed it via github.com/k0kubun/pp master
in go.mod and go mod tidy
, but why it's still different from the master?
How can I use the latest code ?
Thanks.
It seems like it would be a good idea to have an option for a thousands separator for floats and ints, which can make structs with many large numbers easier to read. For example:
myStruct{
myfloat: 1,000,000.00,
}
What do you think?
original: x-motemen/gore#51
If data structure has invalid pointer which is introduced by unsafe package's API or unsafe-related packages, pp crashes by referring the address.
For hiding specific fields in struct:
type SomeObject struct {
someField int // this field will be printed
HiddenField interface{} `pp:"noprint"` // and this one will not.
}
fmt.Printf with %#v
displays []int{}
for empty int slice, and []int(nil)
for nil int slice.
But pp.Printf with %s
displays []int{}
for both of empty int slice and nil int slice.
Is it expected behavior?
Go Playground: https://play.golang.org/p/M-2JZK--7uW
package main
import (
"fmt"
"github.com/k0kubun/pp"
)
func main() {
pp.ColoringEnabled = false
var nilInts []int
pp.Printf("pp.Printf: %s\n", nilInts)
fmt.Printf("fmt.Printf: %#v\n", nilInts)
emptyInts := []int{}
pp.Printf("pp.Printf: %s\n", emptyInts)
fmt.Printf("fmt.Printf: %#v\n", emptyInts)
}
pp.Printf: []int{}
fmt.Printf: []int(nil)
pp.Printf: []int{}
fmt.Printf: []int{}
My structs, when printed look like:
db.Company{
ID: 0x4c58da13a2c8001,
Credits: 0x01f2,
RetryAttempts: 0x0000,
LeadCallerID: 0x0000,
CallRecording: false,
How do I convert?
Sometimes, when printing datastructures that you don't have access to, it's not possible to modify the struct tags. In these situations, sometimes it is nice to be able to ignore unexported fields. Perhaps a flag on the printer, or a global variable (for the default printer) could work?
The problem:
I have a struct with a bunch of populated fields.
I want to print the struct in order to use it in my unit test as data input and not to describe it manually.
The library fits it well, but a few formatting options are missing.
The solution
Add an optional formatting for fields like time.Time and may be the others, instead of raw string value I would like to see:
time.Date(1, 2, 3, 5, 6, 7, time.Local)
Any questions/concerns regarding the feature?
I have
import "github.com/k0kubun/pp"
in the main.go. But go get (with go mod) able to install 3.0.1 only:
go get
go: finding module for package github.com/k0kubun/pp
go: found github.com/k0kubun/pp in github.com/k0kubun/pp v3.0.1+incompatible
Could you please provide support for go mod ? I want to use 3.0.2 seamless
UPD
When using sha of the latest release:
go get github.com/k0kubun/pp@f5c257df338e921f81bfe022aba3d9ba2e00b7b4
go: github.com/k0kubun/pp f5c257df338e921f81bfe022aba3d9ba2e00b7b4 => v1.3.1-0.20200505181502-f5c257df338e
go: downloading github.com/k0kubun/pp v1.3.1-0.20200505181502-f5c257df338e
v1.3.1, what?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.