go-pry

An interactive REPL for Go that allows you to drop into your code at any point.

  • Owner: d4l3k/go-pry
  • Platform:
  • License:: MIT License
  • Category::
  • Topic:
  • Like:
    0
      Compare:

Github stars Tracking Chart

go-pry

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

Build Status
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 .<filename>.gopry and modifies the <filename>.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.

Main metrics

Overview
Name With Ownerd4l3k/go-pry
Primary LanguageGo
Program languageGo (Language Count: 4)
Platform
License:MIT License
所有者活动
Created At2015-03-05 00:10:14
Pushed At2023-02-21 05:42:01
Last Commit At2023-02-20 21:41:52
Release Count0
用户参与
Stargazers Count3k
Watchers Count35
Fork Count63
Commits Count193
Has Issues Enabled
Issues Count41
Issue Open Count7
Pull Requests Count34
Pull Requests Open Count0
Pull Requests Close Count5
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private