GithubHelp home page GithubHelp logo

q's Introduction

q

Go Report Card PkgGoDev

q is a better way to do print statement debugging.

Type q.Q instead of fmt.Printf and your variables will be printed like this:

q output examples

Why is this better than fmt.Printf?

  • Faster to type
  • Pretty-printed vars and expressions
  • Easier to see inside structs
  • Doesn't go to noisy-ass stdout. It goes to $TMPDIR/q.
  • Pretty colors!

Basic Usage

import "q"
...
q.Q(a, b, c)

For best results, dedicate a terminal to tailing $TMPDIR/q while you work.

Install

git clone https://github.com/ryboe/q "$(go env GOPATH)"/src/q

Put these functions in your shell config. Typing qq or rmqq will then start tailing $TMPDIR/q.

qq() {
    clear

    logpath="$TMPDIR/q"
    if [[ -z "$TMPDIR" ]]; then
        logpath="/tmp/q"
    fi

    if [[ ! -f "$logpath" ]]; then
        echo 'Q LOG' > "$logpath"
    fi

    tail -100f -- "$logpath"
}

rmqq() {
    logpath="$TMPDIR/q"
    if [[ -z "$TMPDIR" ]]; then
        logpath="/tmp/q"
    fi
    if [[ -f "$logpath" ]]; then
        rm "$logpath"
    fi
    qq
}

You also can simply tail -f $TMPDIR/q, but it's highly recommended to use the above commands.

Editor Integration

VS Code

Preferences > User Snippets > Go

"qq": {
    "prefix": "qq",
    "body": "q.Q($1) // DEBUG",
    "description": "Pretty-print to $TMPDIR/q"
}

Sublime Text

Tools > Developer > New Snippet

<snippet>
    <content><![CDATA[
q.Q($1) // DEBUG
]]></content>
    <tabTrigger>qq</tabTrigger>
    <scope>source.go</scope>
</snippet>

Atom

Atom > Open Your Snippets

'.source.go':
    'qq':
        'prefix': 'qq'
        'body': 'q.Q($1) // DEBUG'

JetBrains GoLand

Settings > Editor > Live Templates

In Go, add a new template with:

  • Abbreviation: qq
  • Description: Pretty-print to $TMPDIR/q
  • Template text: q.Q($END$) // DEBUG
  • Applicable in: select the Go scope

Emacs

Add a new snippet file to the go-mode snippets directory ($HOME/.emacs.d/snippets/go-mode/qq). This should contain:

# -*- mode: snippet -*-
# name: qq
# key: qq
# --
q.Q(${1:...}) // DEBUG

Vim/NeoVim

For SirVer/ultisnips, use :UltiSnipsEdit to add the new snippet:

snippet qq "qq"
q.Q(${1:})
${2}
endsnippet

Haven't I seen this somewhere before?

Python programmers will recognize this as a Golang port of the q module by zestyping.

Ping does a great job of explaining q in his awesome lightning talk from PyCon 2013. Watch it! It's funny :)

ping's PyCon 2013 lightning talk

FAQ

Why q.Q?

It's quick to type and unlikely to cause naming collisions.

Is q.Q() safe for concurrent use?

Yes.

q's People

Contributors

abirdcfly avatar dependabot-preview[bot] avatar dependabot[bot] avatar dolmen avatar gmwxio avatar hslatman avatar icio avatar johnschnake avatar mattalexander-pantheon avatar nickwells avatar philpennock avatar rafaelsq avatar ryboe avatar savalin avatar sethwklein avatar simonwaldherr avatar tomsquest avatar trizko avatar utrack avatar variadico avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

q's Issues

q explodes my go.mod

add Q to my project explodes my go.mod file with dependencies:

I suspect most of these are linting dependencies. Would there be any way to not have these normally pulled in?

 require (
+       github.com/OpenPeeDeeP/depguard v0.0.0-20181229194401-1f388ab2d810 // indirect
+       github.com/StackExchange/wmi v0.0.0-20181212234831-e0a55b97c705 // indirect
        github.com/benbjohnson/genesis v0.2.1
        github.com/boltdb/bolt v1.3.1 // indirect
+       github.com/coreos/etcd v3.3.11+incompatible // indirect
+       github.com/fatih/color v1.7.0 // indirect
+       github.com/go-lintpack/lintpack v0.5.2 // indirect
+       github.com/go-ole/go-ole v1.2.2 // indirect
+       github.com/go-toolsmith/astcast v1.0.0 // indirect
+       github.com/go-toolsmith/astcopy v1.0.0 // indirect
+       github.com/go-toolsmith/astfmt v1.0.0 // indirect
+       github.com/go-toolsmith/astp v1.0.0 // indirect
+       github.com/go-toolsmith/pkgload v1.0.0 // indirect
+       github.com/go-toolsmith/typep v1.0.0 // indirect
+       github.com/gogo/protobuf v1.2.0 // indirect
+       github.com/golang/mock v1.2.0 // indirect
+       github.com/golangci/errcheck v0.0.0-20181223084120-ef45e06d44b6 // indirect
+       github.com/golangci/gocyclo v0.0.0-20180528144436-0a533e8fa43d // indirect
+       github.com/golangci/gofmt v0.0.0-20181222123516-0b8337e80d98 // indirect
+       github.com/golangci/golangci-lint v1.12.5 // indirect
+       github.com/golangci/lint-1 v0.0.0-20181222135242-d2cdd8c08219 // indirect
+       github.com/golangci/revgrep v0.0.0-20180812185044-276a5c0a1039 // indirect
+       github.com/golangci/tools v0.0.0-20190108095906-d95fd85c147a // indirect
+       github.com/golangci/unparam v0.0.0-20180902115109-7ad9dbcccc16 // indirect
        github.com/gorilla/websocket v1.4.0
+       github.com/kr/pty v1.1.3 // indirect
+       github.com/mattn/go-isatty v0.0.4 // indirect
+       github.com/nbutton23/zxcvbn-go v0.0.0-20180912185939-ae427f1e4c1d // indirect
+       github.com/onsi/ginkgo v1.7.0 // indirect
+       github.com/onsi/gomega v1.4.3 // indirect
+       github.com/pkg/errors v0.8.1 // indirect
        github.com/pkg/term v0.0.0-20181116001808-27bbf2edb814
+       github.com/shirou/gopsutil v2.18.12+incompatible // indirect
+       github.com/shurcooL/go v0.0.0-20181215222900-0143a8f55f04 // indirect
+       github.com/sirupsen/logrus v1.3.0 // indirect
+       github.com/spf13/afero v1.2.0 // indirect
+       github.com/spf13/cobra v0.0.3 // indirect
+       github.com/spf13/viper v1.3.1 // indirect
+       github.com/stretchr/testify v1.3.0 // indirect
        github.com/timshannon/bolthold v0.0.0-20180829183128-83840edea944
+       github.com/ugorji/go/codec v0.0.0-20181209151446-772ced7fd4c2 // indirect
+       github.com/y0ssar1an/q v1.0.6 // indirect
        go.etcd.io/bbolt v1.3.0 // indirect
-       golang.org/x/sys v0.0.0-20181206074257-70b957f3b65e // indirect
+       golang.org/x/crypto v0.0.0-20190103213133-ff983b9c42bc // indirect
+       golang.org/x/image v0.0.0-20181116024801-cd38e8056d9b
+       golang.org/x/net v0.0.0-20190110200230-915654e7eabc // indirect
+       golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4 // indirect
+       golang.org/x/sys v0.0.0-20190109145017-48ac38b7c8cb // indirect
+       golang.org/x/tools v0.0.0-20190111214448-fc1d57b08d7b // indirect
+       gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
        periph.io/x/periph v3.4.0+incompatible
+       sourcegraph.com/sourcegraph/go-diff v0.5.0 // indirect
+       sourcegraph.com/sqs/pbtypes v1.0.0 // indirect
 )

Off/On switch

q is one of my most used tools, so thanks for writing it. I know the project is considered feature complete, but I did want to make you aware of one thing I added that might be one interest, primarily because of its simplicity.

A common need I have is to observe the state inside some common, high-traffic function, which is called by everything, but only when I'm in some code of interest that using that function. Just dropping q in generates a ton of output. To help with this, I added On/Off functions to provide a master switch from the top level. They're so simple I thought I'd make you aware, and can PR if you are interested (and no prob if not).

kalafut@2a2cf04

Thanks again.

use broken with go mod

When tryinq to use q in a project which uses go mod, any command run exits with an error. I am a little suspicious that the issue is really just a bad error message, but I don't quite see how it is getting triggered. Hopefully it make sense to you.

Here is a very simple example that shows the issue.

Create a dir with the following contents in main.go:

// Package main provides an example of q/mod failure
package main

import "github.com/y0ssar1an/q"

func main() {
	i := 1
	t := []int{1, 2, 3}
	q.Q(i, t)
}

run go mod init github.com/reedobrien/w

run go build and get the error:

go: finding github.com/golangci/tools v0.0.0-20180902102414-2cefd77fef9b
go: github.com/golangci/[email protected]: unknown revision 2cefd77fef9b

I don't see golangci or tools in the q repo... so I suspect someone doing something clever vending dependencies, a bug in go mod, or both. Also I've been away from go for a bit and haven't used modules in anger before my current project. So I am a bit lost with them. Hopefully this rings a bell in your head.

defer Q() not printing output.

package main

import (
	. "github.com/y0ssar1an/q"
)

func main() {
	test()
	Q("Third")
}

func test() {
	defer Q("Second")
	Q("First")
}

Produces output

[18:49:47 qtest/main.go:14 main.test]
0.000s First
0.000s

[18:49:47 qtest/main.go:9 main.main]
0.000s Third

Expected

[18:49:47 qtest/main.go:14 main.test]
0.000s First
0.000s Second

[18:49:47 qtest/main.go:9 main.main]
0.000s Third

Should output String()

I see this:

Render p.NodeType=hugolib.NodeType(3)

NodeType is an int, but implements the Stringer interface, so I would expect the String() output to be in there somewhere.

panic in argName

The issue came from the line:

		if a.Obj.Kind == ast.Var || a.Obj.Kind == ast.Con {

I did an output of "%#v %T" right before that and got:

&ast.Ident{NamePos:4893, Name:"tmpTest", Obj:(*ast.Object)(nil)} *ast.Ident

The object in question is a global variable or constant being printed from a test file.
Runnable example:

package main

import 	"github.com/y0ssar1an/q"
var panicInTests string
func main(){
    q.Q(panicInTests) // This is OK
}
package main

import (
	"testing"

	"github.com/y0ssar1an/q"
)

func TestPanicQ(t *testing.T) {
	q.Q(panicInTests) // This will panic
}

Full panic here:

go test
&ast.Ident{NamePos:102, Name:"panicInTests", Obj:(*ast.Object)(nil)} *ast.Ident
--- FAIL: TestPanicQ (0.00s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
	panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x113186f]

goroutine 6 [running]:
testing.tRunner.func1(0xc4200709c0)
	/usr/local/go/src/testing/testing.go:622 +0x29d
panic(0x115a360, 0x1239920)
	/usr/local/go/src/runtime/panic.go:489 +0x2cf
github.com/y0ssar1an/q.argName(0x122ada0, 0xc42000b020, 0x1224c26, 0x44)
	/Users/johnschnake/go/src/github.com/y0ssar1an/q/helpers.go:30 +0x10f
github.com/y0ssar1an/q.argNames.func1(0x1229da0, 0xc420013380, 0x11102c8)
	/Users/johnschnake/go/src/github.com/y0ssar1an/q/helpers.go:79 +0x16e
go/ast.inspector.Visit(0xc42000b0e0, 0x1229da0, 0xc420013380, 0x110220a, 0x0)
	/usr/local/go/src/go/ast/walk.go:373 +0x3a
go/ast.Walk(0x12297a0, 0xc42000b0e0, 0x1229da0, 0xc420013380)
	/usr/local/go/src/go/ast/walk.go:52 +0x66
go/ast.Walk(0x12297a0, 0xc42000b0e0, 0x12f6168, 0xc42000f120)
	/usr/local/go/src/go/ast/walk.go:196 +0x1eb7
go/ast.walkStmtList(0x12297a0, 0xc42000b0e0, 0xc42000f140, 0x1, 0x1)
	/usr/local/go/src/go/ast/walk.go:32 +0x89
go/ast.Walk(0x12297a0, 0xc42000b0e0, 0x1229d60, 0xc420018b10)
	/usr/local/go/src/go/ast/walk.go:224 +0x1f16
go/ast.Walk(0x12297a0, 0xc42000b0e0, 0x12f60d8, 0xc420018b40)
	/usr/local/go/src/go/ast/walk.go:344 +0xf63
go/ast.walkDeclList(0x12297a0, 0xc42000b0e0, 0xc42000b080, 0x2, 0x2)
	/usr/local/go/src/go/ast/walk.go:38 +0x89
go/ast.Walk(0x12297a0, 0xc42000b0e0, 0x1229ee0, 0xc42008ea00)
	/usr/local/go/src/go/ast/walk.go:353 +0x2bb1
go/ast.Inspect(0x1229ee0, 0xc42008ea00, 0xc42000b0e0)
	/usr/local/go/src/go/ast/walk.go:385 +0x4b
github.com/y0ssar1an/q.argNames(0x1224c26, 0x44, 0xa, 0x3, 0x3, 0x4c, 0x0, 0x0)
	/Users/johnschnake/go/src/github.com/y0ssar1an/q/helpers.go:82 +0x29d
github.com/y0ssar1an/q.Q(0xc42002c788, 0x1, 0x1)
	/Users/johnschnake/go/src/github.com/y0ssar1an/q/q.go:175 +0x17c
github.com/johnSchnake/qPanic.TestPanicQ(0xc4200709c0)
	/Users/johnschnake/go/src/github.com/johnSchnake/qPanic/main_test.go:10 +0x7f
testing.tRunner(0xc4200709c0, 0x118f018)
	/usr/local/go/src/testing/testing.go:657 +0x96
created by testing.(*T).Run
	/usr/local/go/src/testing/testing.go:697 +0x2ca
exit status 2
FAIL	github.com/johnSchnake/qPanic	0.011s

go build failed on go1.13+

Using go1.13.7 for building my project (that uses github.com/y0ssar1an/q as a dependency) I got the error:

github.com/y0ssar1an/[email protected] requires
github.com/golangci/[email protected] requires
github.com/golangci/[email protected]: invalid version: unknown revision 2cefd77fef9b

I've cloned the repository, made local changes in go.mod (just set up go1.13)

index 86f936a..092e9a3 100644
--- a/go.mod
+++ b/go.mod
@@ -1,5 +1,5 @@
 module github.com/y0ssar1an/q

-go 1.12
+go 1.13

 require github.com/kr/pretty v0.2.0
(END)

used the 'replace' in my project's go.mod (from github.com/y0ssar1an/q to local copy) - and that helped.

It seems it could be better to use go1.13 in master to avoid problems like this one.

qq log grows forever

Currently, q.log can grow forever. It might be interesting if qq could rotate the log. Maybe we could add a q.MaxFileSize setting or something.

This even might be an issue on Linux, if the temp dir contents get written to RAM instead of disk.

panic when q.Q() an empty *time.Time

q.Q panic when have to print an empty *time.Time

Version: v1.0.17
Steps to reproduce:

var emptyDate *time.Time
q.Q(emptyDate)

Current result:

emptyDate=%!v(PANIC=Format method: value method time.Time.GoString called using nil *Time pointer)

Expected result

emptyDate=(*time.Time)(nil)

Minimise dependencies for the simple case of q.Q()

A lot of stuff gets downloaded for the simple case of adding an import and a q.Q() .
All this can be removed by

  1. imposing a go get ..linter on contributors (removing the vendor dir)
  2. copying a couple of lines from pretty into this code base

PR to follow

Consider moving to golangq/q

Whilst I love the name y0ssar1an, catch-22 is one of my all time favourite books, I need to look up the spelling everytime I use the library. This is kinda counter to the idea behind q.

Hence the suggestion to more the library to something more easily remember.

I have registered github.com/golangq, cloned q & added y0ssar1n as an owner.

panic in prependArgName

panic: runtime error: index out of range

goroutine 1 [running]:
github.com/y0ssar1an/q.prependArgName(0xc420403480, 0x2, 0x2, 0xc42043d0f0, 0x1, 0x1, 0x0, 0x0, 0x2)
	/Users/ryan/go/src/github.com/y0ssar1an/q/helpers.go:156 +0x347
github.com/y0ssar1an/q.Q(0xc4201206c0, 0x1, 0x1)
	/Users/ryan/go/src/github.com/y0ssar1an/q/q.go:182 +0x20a
github.com/apcera/apcera-setup/vendor/github.com/apcera/libretto/virtualmachine/azure/arm.(*VM).deploy(0xc420082340, 0xc4204b4350, 0x5)
	/Users/ryan/go/src/github.com/apcera/apcera-setup/vendor/github.com/apcera/libretto/virtualmachine/azure/arm/util.go:170 +0x30a
github.com/apcera/apcera-setup/vendor/github.com/apcera/libretto/virtualmachine/azure/arm.(*VM).Provision(0xc420082340, 0xc420000180, 0x300000002)
	/Users/ryan/go/src/github.com/apcera/apcera-setup/vendor/github.com/apcera/libretto/virtualmachine/azure/arm/vm.go:135 +0xc1
github.com/apcera/apcera-setup/commands/create.createVM(0xc42047e1e0, 0x1f, 0xc420120ca0)
	/Users/ryan/go/src/github.com/apcera/apcera-setup/commands/create/create.go:368 +0x54
github.com/apcera/apcera-setup/commands/create.createBaseVMs(0xc42028e5a0, 0x3, 0x4, 0x1e49360, 0xc420470630, 0x1e49360, 0xc420470640)
	/Users/ryan/go/src/github.com/apcera/apcera-setup/commands/create/create.go:432 +0x4a3
github.com/apcera/apcera-setup/commands/create.createVMs(0xc42028e5a0, 0x3, 0x4, 0xc4202c0240, 0x0, 0x0, 0x0)
	/Users/ryan/go/src/github.com/apcera/apcera-setup/commands/create/create.go:460 +0xad
github.com/apcera/apcera-setup/commands/create.Run(0xc4202be480, 0x3c6a8f8, 0x0, 0x0, 0xc4202c0240, 0x0, 0x0)
	/Users/ryan/go/src/github.com/apcera/apcera-setup/commands/create/create.go:242 +0x675
github.com/apcera/apcera-setup/commands.ConfiguredCommand.func1(0xc4202be480, 0x3c6a8f8, 0x0, 0x0)
	/Users/ryan/go/src/github.com/apcera/apcera-setup/commands/commands.go:111 +0x1b9
github.com/apcera/apcera-setup/commands.UpdateCheck.func1(0xc4202be480, 0x3c6a8f8, 0x0, 0x0)
	/Users/ryan/go/src/github.com/apcera/apcera-setup/commands/commands.go:169 +0xe5
github.com/apcera/apcera-setup/vendor/github.com/spf13/cobra.(*Command).execute(0xc4202be480, 0x3c6a8f8, 0x0, 0x0, 0xc4202be480, 0x3c6a8f8)
	/Users/ryan/go/src/github.com/apcera/apcera-setup/vendor/github.com/spf13/cobra/command.go:653 +0x2a2
github.com/apcera/apcera-setup/vendor/github.com/spf13/cobra.(*Command).ExecuteC(0x3c41c20, 0x2210ef1, 0xb, 0xc420121f60)
	/Users/ryan/go/src/github.com/apcera/apcera-setup/vendor/github.com/spf13/cobra/command.go:728 +0x2fe
main.main()
	/Users/ryan/go/src/github.com/apcera/apcera-setup/apcera-setup.go:32 +0x38

add ability to also print to stdout

this is really useful for CLi apps also.

so when i do q.Q(variable), it also prints out to the STDout

I coudl tail the temp log, but then i loose control of intermixing.
for example i want everything to go to tmp log but only some things to go to stdout.
I sort of started to use this for nice formatting

https://github.com/olekukonko/tablewriter

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.