Is there a way to access an interactive REPL console in the context of an application, in Go?
For reference, I'm thinking about the functionality of Ruby's irb or pry (and, by extension, the Ruby On Rails console that builds on them), the Node.js REPL, the Scala REPL (and Play console) or Elixir's IEx. All of these are development (or production!) tools to open a REPL in the context of an application. For example, they allow to access an application's classes, objects and constants, and by extension to interact with the application's resources (e.g. a database).
There are some Go REPLs out there, like gore, but it's not clear how to plug them into an application.
I suppose it would be possible to start a gore session and then import all packages, but what about the initializations that happen in the main?
My instinct tells me that there should be a way to provide an alternative "REPL-only" main to compile a different binary that starts the application as a REPL -- rather than, say, an HTTP server.
Is there any established way to do so? Or a reference implementation?
You could try go-pry: https://github.com/d4l3k/go-pry
This works much close to the rails console. Just import "github.com/d4l3k/go-pry/pry" in your file and add pry.Pry() where you want to start your session. After that you just call go-pry main.go instead of go main.go.
But there is not a standard solution, as far as I can tell.
Related
My current code base involves writing console applications in C++ where I import a static library containing many common classes.
I would like to move to Swift and replicate this process. However, I have come across a couple difficulties:
As it stands, one apparently cannot create static libraries in Swift.
As it stands, one apparently can not import a framework into a Swift console application.
Currently I have been experimenting with writing a single view application and importing Frameworks that way, but enough has gone wrong that I would really like to simplify that and stick with console applications.
Given that, so far it appears that my only option for code reuse is to keep Swift files in common directories and drag them as needed into the console application. Since these console applications are for my own use and I'm only interested in the data they generate (i.e. I don't actually give the program to a user) this is actually a workable solution. I was hoping, however, there might be something a bit cleaner.
Any other suggestions on what to do for code reuse for pure Swift console applications, and if so, how do I go about doing it?
You can create a standard Swift framework that contains your common classes, and then create a console application that lives in a bundle. This will allow you to copy the framework into the bundle's framework folder which your console application will have access to at run time.
You can find more details about this approach in Alsey Coleman Miller's article about using embedded frameworks in OS X command line applications.
The downside, of course, is that your console application now lives inside a bundle.
I'm working within the context of RPG Maker VX Ace. It has some Ruby extensions, and it has a subset of the Ruby 1.8 library built in. It also allows me to add my own .rb files -- such as the Ruby 1.9.3 source files -- albeit that I cannot use any code that has C extensions. If it requires a .so file, it won't work -- I'll get an error that "this application cannot load extensions/plugins."
Having understood this, I need some way to make an HTTP request. What I mean is:
Call a URL (eg. http://myserver.com/blah?data=abcd
Return the response (which will be a string -- not full HTML, just an identifier of some sort)
I have tried using open-uri and net/http. Both of them have different issues that prevent me from using. One (I think it's the latter) requires TcpSocket, which is implemented in C and is not "pure" Ruby. The other one gives me an error about an undefined constant related to sockets.
I've also tried using the Socket class, and it gives me a similar error.
TLDR: Is there some way I can make a Ruby HTTP call with pure Ruby, no C extensions? That is what I need.
I solved this by writing some code and calling it from Ruby. Easy breezy.
Using .NET, I created a non-command-line, non-service application; something which I can invoke like a command-line app, but without the command-line window. To do this:
Create a new command-line project
Go to project properties
Change the type to "Windows Forms Application"
When you run your project, it'll run like any command-line project (args, etc.) but no windows will appear.
From there, it was easy to make an asynchronous HTTP request and store the response in a text file. This is sufficient for now.
You could call out to a shell and execute curl (depending on your environment and the speed you want)
result = `curl "http://myserver.com/blah?data=abcd" -s`
The -s makes curl silent. See if that stops the dos box coming up.
The interactive environment is VERY helpful for a programmer. However, it seems Go does not provide it. Is my understanding correct?
No, Go does not provide a REPL(read–eval–print loop).
However, as already mentioned, Go Playground is very handy. The Go Authors are also thinking about adding a feature-rich editor to it.
If you want something local, consider installing hsandbox. Running it simply with hsandbox go will split your terminal screen (with screen) where you can write code at the top and see its execution output at the bottom on every save.
There was a gotry among standard Go commands, which used to evaluate expressions (with an optional package name), and could be run like gotry 1+2 and gotry fmt 'Println("hello")' from shell. It is no longer available because not many people actually used it.
I have also seen third party projects for building a REPL for Go, but now I can only find links to two of them: igo and go-repl. How well do they work I don't know.
My two cents: Speed of compilation makes writing a REPL possible for Go, as it has also helped building the tools mentioned here, but the same speed makes REPL less necessary. Every time I want to test something in Go that I can't run in Playground I open a simple .go file and start coding and simply run the code. This will be even easier when the go command in Go 1 makes one-command build process possible and way easier.
UPDATE: Latest weekly release of Go added go command which can be used to very easily build a file: write your prog.go file and run go build prog.go && ./prog
UPDATE 2: With Go 1 you can directly run go programs with go run filename.go
UPDATE 3: gore is a new project which seems interesting.
Try motemen/gore
Yet another Go REPL that works nicely. Featured with line editing,
code completion, and more.
https://github.com/motemen/gore
You also have a recent (March 2013) project called gore from Sriram Srinivasan, which can be useful:
gore is a command-line evaluator for golang code -- a REPL without a loop, if you will.
It is a replacement for the go playground, while making it much easier to interactively try out bits of code: gore automatically supplies boiler-plate code such as import and package declarations and a main function wrapper.
Also, since it runs on your own computer, no code is rejected on security grounds (unlike go playground's safe sandbox mode).
If you're a Vim user, the vim-go plugin (https://github.com/fatih/vim-go) provides a command (GoRun) to run and print the output of the current buffer. You still have to include all the boilerplate code of a main Go file, but it still provides a convenient way to quickly test code snippets in your local environment.
Have you tried the Go Playground?
About the Go Playground
The Go Playground is a web service that runs on golang.org's servers.
The service receives a Go program, compiles, links, and runs the
program inside a sandbox, then returns the output.
The GoSpeccy project includes a builtin REPL of a restricted subset of the Go language. The implementation is using goeval.
No, but you can exploit the speed of compilation (as mentioned in other answers).
Have a look at rango that uses a generate-compile-run loop to mimic a REPL. You can also start it with imports and statements to begin an interactive session.
Gosh is the interactive Golang shell. The goal is to provide an easy-to-use interactive execution environment.
https://github.com/mkouhei/gosh
I've had some luck with the VSCode debugger, but it's fairly limited in so far as you cannot invoke function calls from the debug console Debug: Function Calls not supported #2225.
Basically you set a breakpoint after properly configuring your launch.json file. Then you can drill down on the left in the variables side bar and enter variable expressions an the debug console.
You may also like to try https://github.com/haya14busa/goplay
This enables you to run go code files from your terminal directly to the Go Playground
Please also check www.gorepl.com for go REPL and other REPLs
Go code can be run in a REPL-like way in Visual Studio Code with the Go extension and Code Runner extension. Click the Run triangle ▶ which is marked by the mouse cursor in the below screenshot to run the code and show the results in the Output pane at the bottom of Visual Studio Code.
When programming with Go Visual Studio Code will suggest additional Go extensions that can be installed to extend Visual Studio Code's functionality.
I've written a little library that uses implicits to add functionality that one only needs when using the REPL in Scala. Ruby has libraries like this - for things like pretty printing, firing up text editors (like the interactive_editor gem which invokes Vim from irb - see this post), debuggers and the like. The library I am trying to write adds some methods to java.lang.Class and java.lang.reflect classes using the 'pimp my library' implicit conversion process to help you go and find documentation (initially, with Google, then later possibly with a JavaDoc/ScalaDoc viewer, and maybe the StackOverflow API eventually!). It's an itch-scratching library: I spend so much time copying and pasting classnames into Google that I figured I may as well automate the process.
It is the sort of functionality that developers will want to add to their system for use only in the REPL - they shouldn't really be adding it to projects (partly because it may not be something that their fellow developers want, but also because if you are doing some exploratory development, it may be with just a Scala REPL that's not being invoked by an IDE or build tool).
In my case, I want to include a few classes and set up some implicits - include a .jar on the CLASSPATH and import it, basically.
In Ruby, this is the sort of thing that you'd add to your .irbrc file. Other REPLs have similar ways of setting options and importing libraries.
Is there a similar file or way of doing this for the Scala REPL?
On the command line, you can use the -i option to load a file while starting the REPL:
scala -cp mystuff.jar -i mydefs.scala
Ofcourse you could wrap this in a shell script or batch file and run that instead of the normal scala command.
(I'm using Scala 2.8.0 RC3).
Not sure if this is what you are looking for, but if you put any jars in your SCALA_HOME\lib directory. Then those jars will be available for import in the REPL (using the import keyword).
EDIT: The most convenient option as of now is by setting the CLASSPATH environment variable. Any jars referenced in the CLASSPATH variable are also available for import in the REPL.
Quick answer probably not what you are looking for, but what about typing
:load path/to/some/scala/script/file.scala
in the console?
:load will read in a scala file and execute it as a script.
Another option is to use sbt set up your dependencies and execute the console command.
The final option I can think of is to set the classpath on the command line manually and point it to the jars / class file folders that you want the jvm to know about.
Let me know if any of this interests you and I can provide more details if needed.
In Java when you compile a .java file which defines a class, it creates a .class file. If you provide these class files to your coworkers then they cannot modify your source. You can also bundle all of these class files into a jar file to package it up more neatly and distribute it as a single library.
Does Ruby have any features like these when you want to share your functionality with your coworkers but you don't want them to be able to modify the source (unless they ask you for the actual .rb source file and tell you that they want to change it)?
I believe the feature you are looking for is called "trust" (and a source code control repository). Ruby isn't compiled in the same way that Java is, so no you can't do this.
I have to say your are in a rough position, not wanting to share code with a coworker. However, given that this is an unassailable constraint perhaps you could change the nature of the problem.
If you have a coworker that needs access to some service provided by a library of yours, perhaps you could expose it by providing a web/rest service instead of as a .rb file.
This way you can hide your code behind a web server, and if there is a network architecture that allows for low latency making these service calls, you can effectively achive the same goal.
Trust is a lot easier though.
edit:
Just saw this on HN: http://blog.astrails.com/2009/5/12/ruby-http-require, allows a ruby file to include another file through http instead of the filesystem.
Ruby is
A dynamic, interpreted, open source programming language with a focus on simplicity and productivity.
So like all interpreted languages, you need to give the source code to anyone who want's to execute your program/script.
By the way searching "compiled ruby" on google returned quiet a few results.
I don't think there is one. Ruby is purely an interpreted language, which means ruby interprets your source code directly in order to run it. Java is compiled, so there's an intermediate bytecode (the .class). You can obfuscate your ruby if you really wish, but it's probably more trouble than it's worth.
Just to make sure you realize, however, upwards of 95% of Java can be decompiled back into source using various free utilities, so in reality, Java's compilation isn't much better than distributing Ruby source.
This is not a language specific problem and one that can be managed more effectively through source control software.
There is a library called ruby2c that compiles a subset of Ruby into C code (which you can then compile into native code, if you want).
It was actually originally written as a Ruby code obfuscator (but has since been used for lots of other stuff, including Ruby Arduino development).