go-pry - an interactive REPL for Go that allows you to drop into your code at any point.

Overview

go-pry

go-pry - an interactive REPL for Go that allows you to drop into your code at any point.

Tests GoDoc

go-pry

Example

go-pry Animated Example go-pry Example

Usage

Install go-pry

go get github.com/d4l3k/go-pry
go install -i github.com/d4l3k/go-pry

Add the pry statement to the code

package main

import "github.com/d4l3k/go-pry/pry"

func main() {
  a := 1
  pry.Pry()
}

Run the code as you would normally with the go command. go-pry is just a wrapper.

# Run
go-pry run readme.go

If you want completions to work properly, also install gocode if it is not installed in your system

go get -u github.com/nsf/gocode

How does it work?

go-pry is built using a combination of meta programming as well as a massive amount of reflection. When you invoke the go-pry command it looks at the Go files in the mentioned directories (or the current in cases such as go-pry build) and processes them. Since Go is a compiled language there's no way to dynamically get in scope variables, and even if there was, unused imports would be automatically removed for optimization purposes. Thus, go-pry has to find every instance of pry.Pry() and inject a large blob of code that contains references to all in scope variables and functions as well as those of the imported packages. When doing this it makes a copy of your file to . .gopry and modifies the .go then passes the command arguments to the standard go command. Once the command exits, it restores the files.

If the program unexpectedly fails there is a custom command go-pry restore [files] that will move the files back. An alternative is to just remove the pry.Apply(...) line.

Inspiration

go-pry is greatly inspired by Pry REPL for Ruby.

License

go-pry is licensed under the MIT license.

Made by Tristan Rice.

Comments
  • Is go-pry supposed to run standalone

    Is go-pry supposed to run standalone

    Apologies if this is already explained elsewhere, but it's not clear from the examples in the readme if it's possible to run go-pry as a standalone REPL.

    When I try I get this error, but I'm not sure if it's the normal behaviour or a problem with my setup.

    $ go get github.com/d4l3k/go-pry
    $ go install github.com/d4l3k/go-pry
    $ which go-pry
    ....../go/bin/go-pry
    $ go-pry -i="fmt,math,strconv"
    panic: failed to TypeCheck: /var/folders/mw/hh951k897lv1c8nchgxyy9zw0000gp/T/pry101925011/main.go:3:2: could not import github.com/d4l3k/go-pry/pry (can't find import: "github.com/d4l3k/go-pry/pry")
    
    goroutine 1 [running]:
    github.com/d4l3k/go-pry/pry.Apply(0xc4200ba200)
        /Users/Tom/source/go/src/github.com/d4l3k/go-pry/pry/pry.go:86 +0x228
    main.main()
        /var/folders/mw/hh951k897lv1c8nchgxyy9zw0000gp/T/pry101925011/main.go:9 +0x3217
    exit status 2
    
    opened by tompave 13
  • can't find golang.org/x/tools/go/gcimporter15

    can't find golang.org/x/tools/go/gcimporter15

    when i used this package, get this error message.

    panic: cannot find package "golang.org/x/tools/go/gcimporter15" in any of:
    	/usr/local/Cellar/go/1.10/libexec/src/golang.org/x/tools/go/gcimporter15 (from $GOROOT)
    

    I created PR that fixd the place I think fix it. But I did not know how to test this project. If there is any shortage, please tell me or fix it.

    sorry my poor english

    opened by j-o-lantern0422 10
  • cannot find package

    cannot find package "foo"

    I'm trying to use go-pry to debug this golang project-> https://github.com/komuw/meli But go-pry is panicking with a weird error, reproduced below:

    go-pry run *.go

    panic: cannot find package "foo" in any of:
    	/home/komu/go/src/github.com/komuw/meli/vendor/foo (vendor tree)
    	/usr/local/go/src/foo (from $GOROOT)
    	/home/komu/go/src/foo (from $GOPATH)
    
    goroutine 1 [running]:
    main.InjectPry(0xc420214c00, 0x60, 0x6c8703, 0x3, 0x1, 0xc42042f6d8)
    	/home/komu/go/src/github.com/d4l3k/go-pry/main.go:65 +0x14c0
    main.main.func4(0xc423200740, 0x38, 0x80cde0, 0xc420231930, 0x0, 0x0, 0x0, 0x0)
    	/home/komu/go/src/github.com/d4l3k/go-pry/main.go:252 +0x140
    path/filepath.walk(0xc423200740, 0x38, 0x80cde0, 0xc420231930, 0xc4200ea030, 0x0, 0x0)
    	/usr/local/go/src/path/filepath/path.go:356 +0x81
    path/filepath.walk(0xc4233a2ea0, 0x30, 0x80cde0, 0xc420231860, 0xc4200ea030, 0x0, 0x0)
    	/usr/local/go/src/path/filepath/path.go:381 +0x39a
    path/filepath.walk(0xc421eb60f0, 0x26, 0x80cde0, 0xc4221a4c30, 0xc4200ea030, 0x0, 0x0)
    	/usr/local/go/src/path/filepath/path.go:381 +0x39a
    path/filepath.walk(0xc422661e00, 0x1e, 0x80cde0, 0xc4221a4750, 0xc4200ea030, 0x0, 0x0)
    	/usr/local/go/src/path/filepath/path.go:381 +0x39a
    path/filepath.walk(0xc422661da0, 0x17, 0x80cde0, 0xc4221a4680, 0xc4200ea030, 0x0, 0x0)
    	/usr/local/go/src/path/filepath/path.go:381 +0x39a
    path/filepath.walk(0xc4205932a0, 0x11, 0x80cde0, 0xc420426dd0, 0xc4200ea030, 0x0, 0x0)
    	/usr/local/go/src/path/filepath/path.go:381 +0x39a
    path/filepath.walk(0xc420216288, 0x6, 0x80cde0, 0xc420426d00, 0xc4200ea030, 0x0, 0x0)
    	/usr/local/go/src/path/filepath/path.go:381 +0x39a
    path/filepath.walk(0x6c8554, 0x1, 0x80cde0, 0xc4200ec0d0, 0xc4200ea030, 0x0, 0x30)
    	/usr/local/go/src/path/filepath/path.go:381 +0x39a
    path/filepath.Walk(0x6c8554, 0x1, 0xc4200ea030, 0x2, 0x3)
    	/usr/local/go/src/path/filepath/path.go:403 +0x11d
    main.main()
    	/home/komu/go/src/github.com/d4l3k/go-pry/main.go:243 +0x53e
    

    I'm using dep(https://github.com/golang/dep) as my dependency management tool

    opened by komuw 8
  • Packages in vendor/ are not found

    Packages in vendor/ are not found

    Hey, I tried to use go-pry in a project where external packages are below vendor/. Go pry keeps saying packages not found:

    panic: cannot find package "github.com/google/go-github/github" in any of:
    	/usr/local/go/src/github.com/google/go-github/github (from $GOROOT)
    	/src/github.com/google/go-github/github (from $GOPATH)
    

    it's actually in /src//vendor/github.com/google/go-github/github

    Could you give me some pointers how to fix this pls?

    opened by rngtng 8
  • Question/Discussion: REPL history + copy-paste

    Question/Discussion: REPL history + copy-paste

    First: thanks for doing this, this it the most functional go repl out there!

    While not a big issue, but it would be nice to be able to copy-paste larger code blocks into the repl directly. History + tab completion would be the cherry on top of it.

    Here is a project that used the go impl. of readline with great success, maybe this could be something useful here?

    https://github.com/abiosoft/ishell

    Cheers Roman

    opened by mindreframer 6
  • Error running go-pry: panic: interface conversion: interface is nil, not reflect.Type

    Error running go-pry: panic: interface conversion: interface is nil, not reflect.Type

    Hi, I was trying to use this amazing project but stumbled upon the issue. Here is output that I'm getting:

    panic: interface conversion: interface is nil, not reflect.Type                                                                                                                                                                                                                 
    
    goroutine 1 [running]:
    github.com/d4l3k/go-pry/pry.(*Scope).Interpret(0xc421ac8a20, 0x7feb32653268, 0xc42010e600, 0x7feb32653268, 0xc42010e600, 0x7d9540, 0xc420102620)
            /home/gnzh/go-projects/src/github.com/d4l3k/go-pry/pry/interpreter.go:350 +0x24a3
    main.GetExports(0xc4202305f8, 0x6, 0xc420d04c00, 0xc4203174a0, 0xc4203174a0, 0x6c4084)
            /home/gnzh/go-projects/src/github.com/d4l3k/go-pry/main.go:338 +0x7ce
    main.InjectPry(0xc4203242a0, 0x60, 0x6be3c3, 0x3, 0x300000001, 0xc4200001a0)
            /home/gnzh/go-projects/src/github.com/d4l3k/go-pry/main.go:77 +0x60c
    main.main.func4(0xc420dfac80, 0x3c, 0x7dd1a0, 0xc420dfc0d0, 0x0, 0x0, 0x0, 0x0)
            /home/gnzh/go-projects/src/github.com/d4l3k/go-pry/main.go:251 +0xdd
    path/filepath.walk(0xc420dfac80, 0x3c, 0x7dd1a0, 0xc420dfc0d0, 0xc420080db0, 0x0, 0x0)
            /usr/lib/go/src/path/filepath/path.go:351 +0x81
    path/filepath.walk(0xc420dfabc0, 0x34, 0x7dd1a0, 0xc420dfc000, 0xc420080db0, 0x0, 0x0)
            /usr/lib/go/src/path/filepath/path.go:376 +0x414
    path/filepath.walk(0xc42010e120, 0x2d, 0x7dd1a0, 0xc420df4340, 0xc420080db0, 0x0, 0x0)
            /usr/lib/go/src/path/filepath/path.go:376 +0x414
    path/filepath.walk(0xc420105ad0, 0x24, 0x7dd1a0, 0xc420259ee0, 0xc420080db0, 0x0, 0x0)
            /usr/lib/go/src/path/filepath/path.go:376 +0x414
    path/filepath.walk(0xc420cc0940, 0x17, 0x7dd1a0, 0xc420259e10, 0xc420080db0, 0x0, 0x0)
            /usr/lib/go/src/path/filepath/path.go:376 +0x414
    path/filepath.walk(0xc420254420, 0x11, 0x7dd1a0, 0xc4202525b0, 0xc420080db0, 0x0, 0x0)
            /usr/lib/go/src/path/filepath/path.go:376 +0x414
    path/filepath.walk(0xc420249388, 0x6, 0x7dd1a0, 0xc4202524e0, 0xc420080db0, 0x0, 0x0)
            /usr/lib/go/src/path/filepath/path.go:376 +0x414
    path/filepath.walk(0x6be214, 0x1, 0x7dd1a0, 0xc420085860, 0xc420080db0, 0x0, 0x30)
            /usr/lib/go/src/path/filepath/path.go:376 +0x414
    path/filepath.Walk(0x6be214, 0x1, 0xc420080db0, 0x0, 0x1)
            /usr/lib/go/src/path/filepath/path.go:398 +0x14c
    main.main()
            /home/gnzh/go-projects/src/github.com/d4l3k/go-pry/main.go:259 +0x63d
    

    I'm running go version go1.8.3 linux/amd64 on Arch Linux.

    Here is the smallest project that I was able to reproduce the issue in:

    https://github.com/Gonzih/go-pry-panic-issue-reproduction-example

    Please feel free to ask for any extra information on my setup.

    Thanks a lot!

    opened by Gonzih 6
  • seems like Doesn't Support Go 1.11 Go Module

    seems like Doesn't Support Go 1.11 Go Module

    have a project using Go 1.11 Go Module with go.mod:

    #file go.mod
    module just-go
    require(
      blah blah blah...
    )
    

    main file like this:

    //file main.go
    package main
    
    import (
    	"fmt"
    	"github.com/d4l3k/go-pry/pry"
    	"just-go/db"
    )
    
    func main() {
    	fmt.Printf("%v\n", db.DB)
    	fmt.Println("starting debug....")
    	pry.Pry()
    }
    

    run pry

    $ go-pry run main.go
    
    2018/12/25 20:18:52 main.go:28: cannot find package "just-go/db" in any of:
            /Users/jojo/.gvm/gos/go1.11.1/src/just-go/db (from $GOROOT)
            /Users/jojo/.gvm/pkgsets/go1.11.1/global/src/just-go/db (from $GOPATH)
    

    as i use the old way set the source path into GOPATH, got this:

    $ ln -s just-go $GOPATH/src
    $ go-pry run main.go
    
    &{<nil> <nil> 0 0xc0000d2480 false 2 {0xc000185180} <nil> {{0 0} {<nil>} map[] 0} 0xc000118c60 0x17cde40 0xc0001c28a0 false}
    starting debug....
    panic: failed to TypeCheck: /Users/jojo/yiwu/just-go/tasks/debug/debug.go:5:2: could not import github.com/d4l3k/go-pry/pry (can't find import: "github.com/d4l3k/go-pry/pry")
    
    goroutine 1 [running]:
    github.com/d4l3k/go-pry/pry.Apply(0xc0001e8100)
            /Users/jojo/.gvm/pkgsets/go1.11.1/global/pkg/mod/github.com/d4l3k/[email protected]/pry/pry.go:28 +0x220
    main.main()
            /Users/jojo/yiwu/just-go/main.go:12 +0x198a
    exit status 2
    2018/12/25 20:21:26 main.go:28: exit status 1
    

    Go Module has a new way to put the compiled pkg into the $GOPATH/pkg/mod, and no longer use the $GOPATH/src, any other idea to fix this problem ?

    opened by reducm 5
  • go-pry> panic: rpc: can't find service RPC.RPC_auto_complete

    go-pry> panic: rpc: can't find service RPC.RPC_auto_complete

    Hi, I try the example, but it panic

    [8] go-pry> panic: rpc: can't find service RPC.RPC_auto_complete
    
    goroutine 1 [running]:
    main.client_auto_complete(0xc000160240, 0xc0001c4000, 0x4768, 0x7e00, 0x7ffeefbfee14, 0x3c, 0x2428, 0x14bf5a4, 0x5, 0x14c03ef, ...)
    /Users/pathbox/gowork/pkg/mod/github.com/nsf/[email protected]/rpc.go:38 +0x20a
    main.cmd_auto_complete(0xc000160240)
    	/Users/pathbox/gowork/pkg/mod/github.com/nsf/[email protected]/client.go:164 +0x38f
    main.do_client(0x0)
    	/Users/pathbox/gowork/pkg/mod/github.com/nsf/[email protected]/client.go:44 +0x35a
    main.main()
    	/Users/pathbox/gowork/pkg/mod/github.com/nsf/[email protected]/gocode.go:71 +0x82
    

    I don't konw the relations with gocode

    opened by pathbox 4
  • Keys() method's return statement in interpreter.go does not appear to return ( keys []string)

    Keys() method's return statement in interpreter.go does not appear to return ( keys []string)

    Hi D413k,

    If I read your code correctly you Keys method (in interpreter.go file) does not return a (keys []string) in body of method. Please review accordingly (see excerpt below).

    // Keys returns all keys in scope
    func (scope *Scope) Keys() (keys []string) {
        currentScope := scope
        for currentScope != nil {
            for k := range currentScope.Vals {
                keys = append(keys, k)
            }
            currentScope = scope.Parent
        }
        return
    }
    
    opened by kkruups 4
  • Can't install

    Can't install

    [email protected]:~/go/src/github.com/househappy/hyper_sonic$ go get github.com/d4l3k/go-pry
    # github.com/d4l3k/go-pry
    ../../d4l3k/go-pry/main.go:162: syntax error: unexpected range, expecting {
    ../../d4l3k/go-pry/main.go:167: non-declaration statement outside function body
    ../../d4l3k/go-pry/main.go:168: non-declaration statement outside function body
    ../../d4l3k/go-pry/main.go:169: non-declaration statement outside function body
    ../../d4l3k/go-pry/main.go:180: non-declaration statement outside function body
    ../../d4l3k/go-pry/main.go:181: non-declaration statement outside function body
    ../../d4l3k/go-pry/main.go:182: non-declaration statement outside function body
    ../../d4l3k/go-pry/main.go:189: non-declaration statement outside function body
    ../../d4l3k/go-pry/main.go:191: non-declaration statement outside function body
    ../../d4l3k/go-pry/main.go:196: non-declaration statement outside function body
    ../../d4l3k/go-pry/main.go:196: too many errors
    
    opened by hackervera 4
  • Refactor: implement History as an interface

    Refactor: implement History as an interface

    Hey @d4l3k

    In my spare time, I checked out your project. Really thanks for doing it! Here is a bit of my refactoring. The motivation was to make the code a bit structured and I picked a history functionality due to the simplicity of it. Hope you would find it useful.

    NOTE: i did not rename io_default.go, io_js.go to keep the diff understable and clear.

    opened by tsyber1an 3
  • Working with Echo

    Working with Echo

    Hi, everyone! Could you please tell me, can we use go-pry with Echo framework https://github.com/labstack/echo? I've added pry.Pry() to my code, but didn't have result. I've used go-pry instead of go.

    opened by kalashnikovisme 0
  • How about adding debug commands such likes continue or run?

    How about adding debug commands such likes continue or run?

    Sorry for not providing details. Here are what I mentioned. Wish to add some debug related commands for us to debug as python(ipdb) or ruby(pry)

    
         5:   "github.com/d4l3k/go-pry/pry"
         6: )
         7:
         8: func main() {
         9:   a := 1
     => 10:   pry.Pry()
        11:   fmt.Println("A", a)
        12: }
        13:
    
    [31] go-pry> a = 2
    => 2
    [32] go-pry>
    

    I modify the var a value to 2, and willing to execute next or continue commands to continue the program.

    Is there any methods to add those to go-pry? I have noticed what you have mentioned below it just has wrap go commands. So i thought it is impossible to add those commands.

    enhancement 
    opened by jkryanchou 7
  • Access to unexported

    Access to unexported

    reflect panic when trying to access an unexported field in a struct. For example :

    type A struct {
      unexp string
      Exp    string
    }
    a := &A{/*.....*/}
    

    Access to a.unexp will panic. Problem is here..

    https://github.com/d4l3k/go-pry/blob/f6e6fbd4789735a7b691b4d699db59695994d3dd/pry/interpreter.go#L226

    I believe there should be a check on field.isValid() AND sel.isExported() or not. If exported then field.Interface(), otherwise need to do something like this.

        // ......
        if field.IsValid() && sel.IsExported() {
          return field.Interface(), nil
        } else if field.IsValid() && !sel.IsExported() {
          k := rVal.FieldByName(sel.Name)
          if k.CanAddr() { // can address
            k = reflect.NewAt(k.Type(), unsafe.Pointer(k.UnsafeAddr())).Elem()
          } else { // can't address
            switch k.Kind() {
            case reflect.String:
              return k.String(), nil
            // ......
            default:
              return k.Interface(), nil
            }   
          }
          return k.Interface(), nil
        }
    
    enhancement 
    opened by q6r 1
  • Access the latest value

    Access the latest value

    Assign the latest value to a variable, _ for instance, so that you can build on whatever resulted in the last command without having to arrow-up and edit it.

    enhancement 
    opened by JJ 1
  • Ability to run go-pry outside of main package

    Ability to run go-pry outside of main package

    I'm wondering if it's in the scope of go-pry to eventually allow it to run in non-main packages. The way I currently have my Go project set up is to have a main package that sources certain functionalities from a ton of subpackages.

    opened by catherinetcai 4
Owner
Tristan Rice
Hi! Distributed ML @pytorch
Tristan Rice
Generate code for any language, with any language.

gocog - generate code for any language, with any language gocog v1.0 build 20130206 Binaries for popular OSes are available on the Downloads page of t

Nate Finch 66 Aug 5, 2022
Aegis is a library that allows you detect if your software is being debugged or not on Linux, FreeBSD, NetBSD, OpenBSD and Windows

Aegis is a library that allows you detect if your software is being debugged or not on Linux, FreeBSD, NetBSD, OpenBSD and Windows. You can use it natively from C or use the Go bind.

Rafael Santiago 28 Aug 29, 2022
A tool that helps you write code in your favorite IDE: your word processor!

WordIDE Have you ever wondered: How would it feel like to write code in a word processor? Me neither. But after months minutes of planning, I present

unsafecast 35 Jul 21, 2022
Will autobuild and kill/relaunch the target when you update the code.

Use like rerun github.com/skelterjohn/go.uik/uiktest Usage: rerun [--test] [--build] [--race] [--no-run] <import path> [arg]* For any go executable in

John Asmuth 258 Jul 23, 2022
A small tool that allows a process to ask a debugger to attach to it.

Client and server for a process to request attach by gdlv. These two packages allow a program to request that a debugger attach to it. The motivating

David Chase 0 Feb 1, 2022
🔥 Continuous profiling platform — debug performance issues in your code!

Pyroscope is an open source continuous profiling platform.

Pyroscope 6.9k Jan 7, 2023
📦 Package Node.js applications into executable binaries 📦

caxa ?? Package Node.js applications into executable binaries ?? Support Recurring support on Patreon: https://patreon.com/leafac One-time support on

Leandro Facchinetti 616 Jan 6, 2023
A program to build, run, and restart a Go program on code change

devrun A program to build, run, and restart a Go program on code change. It also supports watching all your Go imports too. So if you change the code

Kevin Darlington 19 Apr 4, 2022
Go package for syntax highlighting of code

syntaxhighlight Package syntaxhighlight provides syntax highlighting for code. It currently uses a language-independent lexer and performs decently on

Sourcegraph 253 Nov 18, 2022
Simple tool that updates Visual Studio Code workspace(s) to include Go modules in gopath/src, then launches VSCode if only one modified.

Simple tool that updates Visual Studio Code workspace(s) to include Go modules in gopath/src, then launches VSCode if only one modified.

Valérie 0 Jan 27, 2022
NoColor — validate the architecture of your PHP project

NoColor is an architecture validation tool based on the concept of colored functions. This concept was originally invented for KPHP and later exposed as a separate tool to be used in regular PHP projects.

VK.com 149 Nov 28, 2022
REPLbot is a Slack bot that allows you to control a REPL from within Slack.

?? REPLbot REPLbot is a Slack bot that allows you to control a REPL from within Slack. It comes with a few REPLs (Go ?? , Java, NodeJS, PHP, Python, R

Philipp C. Heckel 182 Dec 31, 2022
Interactive Go interpreter and debugger with REPL, Eval, generics and Lisp-like macros

gomacro - interactive Go interpreter and debugger with generics and macros gomacro is an almost complete Go interpreter, implemented in pure Go. It of

Massimiliano Ghilardi 2k Dec 30, 2022
Go (golang) Jupyter Notebook kernel and an interactive REPL

lgo Go (golang) Jupyter Notebook kernel and an interactive REPL Disclaimer Since go1.10, this Go kernel has performance issue due to a performance reg

Yu Watanabe 2.3k Jan 1, 2023
An anonymous, encrypted Point-to-Point (Layer 3) tunnel between two peers.

NKN-Link An anonymous, encrypted Point-to-Point (Layer 3) tunnel between two peers. NKN-Link Table of Contents Preface Description Install Setup Run P

HAH! Sun 8 Dec 20, 2022
Wg-configurator - This project makes it easier to configure a fleet of servers interconnected via the WireGuard point-to-point virtual private network.

WireGuard Configurator This project makes it easier to configure a fleet of servers interconnected via the WireGuard point-to-point virtual private ne

Caleb L. Power 1 Mar 29, 2022
Yet another Go REPL that works nicely. Featured with line editing, code completion, and more.

gore Yet another Go REPL that works nicely. Featured with line editing, code completion, and more. (Screencast taken with cho45/KeyCast) Usage gore Af

Hironao OTSUBO 4.7k Jan 2, 2023
Yet another Go REPL that works nicely. Featured with line editing, code completion, and more.

gore Yet another Go REPL that works nicely. Featured with line editing, code completion, and more. (Screencast taken with cho45/KeyCast) Usage gore Af

Hironao OTSUBO 4.7k Jan 7, 2023
Nodebook - Multi-Lang Web REPL + CLI Code runner

nodebook Nodebook - Multi-Language REPL with Web UI + CLI code runner Useful to practice algorithms and datastructures for coding interviews. What is

Jérôme Schneider 1.6k Dec 29, 2022
A drop-in replacement to any Writer type, which also calculates a hash using the provided hash type.

writehasher A drop-in replacement to any Writer type, which also calculates a hash using the provided hash type. Example package main import ( "fmt"

Max 0 Jan 10, 2022