FAQ
TL:DR;

Code:
someDescriptiveNameOfVar := 42
goon.DumpExpr <http://godoc.org/github.com/shurcooL/go-goon#DumpExpr>
(someDescriptiveNameOfVar)

Output:
someDescriptiveNameOfVar = (int)(42)

Long Version

Go already makes adding debug printf statements much easier than in C++ and
such, because you can use %v for all types, or, just use
fmt.Print/Println() funcs and skip specifying types. But you may still want
to add some string that describes the values you're printing...

Say you quickly add a debug print into some code:

size := uint64(v.(ast.Node).End() - v.(ast.Node).Pos())
if size < smallest {
fmt.Println(size, smallest) // Quickly added this to see the values
smallestV = v
smallest = size
}

You may see this output:

43 27

And forget which is which. So you may change it to:

fmt.Printf("smallest=%v, size=%v", size, smallest)

And not realize you've made a mistake in the naming...

---

As of 2467f1d961<https://github.com/shurcooL/go-goon/commit/2467f1d961a78028133d1afe581f68204521cbac>,
I've added func DumpExpr(a ...interface{}) (and a SdumpExpr() version that
returns a string) to "github.com/shurcooL/go-goon"<http://godoc.org/github.com/shurcooL/go-goon>
.

So you can do this:

import "github.com/shurcooL/go-goon"<http://godoc.org/github.com/shurcooL/go-goon>
var _ = goon.DumpExpr // Optional, avoids "imported but not used" errors

goon.DumpExpr(smallest, size)

And get this output:

smallest = (uint64)(1)
size = (uint64)(1)

Caveats: It needs the source code to be present in the original location
where it was compiled from. So `go run prog.go` will work. `go
build prog.go && ./prog` will also work. But `go build prog.go && mv
prog.go prog2.go && ./prog` will not. Since this is primarily for quick
debugging needs, it should be fine.

How it works: It uses runtime.Stack() to figure out the file and line of
parent func calls, then reads the filepath that stack returns, parses its
AST, finds the parameters that were given to goon.DumpExpr() call, and uses
them as names. See http://godoc.org/gist.github.com/6418290.git

I'm still unsure if I'll keep the DumpExpr() func name, move this
functionality into Dump(), or come up with a better name. Suggestions are
welcome.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Dmitri Shuralyov at Oct 26, 2013 at 7:37 am
    You can print any expression, so even things like this:

    goon.DumpExpr(smallestNode.Pos() - 1)

    Will print:

    smallestNode.Pos() - 1 = (token.Pos)(91)

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedOct 26, '13 at 7:31a
activeOct 26, '13 at 7:37a
posts2
users1
websitegolang.org

1 user in discussion

Dmitri Shuralyov: 2 posts

People

Translate

site design / logo © 2021 Grokbase