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.
Related
I am starting to learn Go following this Microsoft tutorial. The program runs and displays the result as in the tutorial, but the two problems that it is marking me cause me concern. I do not want to continue without understanding what this detail is due to, someone who has also happened to it, or who helps me to know what it is due to, I will be very grateful.
package main
import (
"fmt"
"github.com/x0z38/calculator"
"rsc.io/quote"
)
func main(){
total := calculator.Sum(3, 5)
fmt.Println(total)
fmt.Println("Version: ", calculator.Version)
fmt.Println(quote.Hello())
}
I leave you the image where the error is marked in red lines in the editor:
I leave the image of the two problems:
According to what I understood is that it does not find those files in any of the mentioned paths, but both files if I have them inside this path: C:\Projects\Go\src.
My GOPATH environment variable is: C:\Projects\Go
Golang has two ways to manage dependencies: old and new. Switching between them is usually done automatically.
Visual Sudio Code tries to check for dependencies using the old way. But I see you have go.mod and go.sum files which means you are using the new way (the Golang module system).
The environment variable GO111MODULE is used to switch between dependency control modes. It has 3 values: auto, on, off. The default is auto.
What you see is just a syntax highlighting problem and not a compilation or execution error.
What I have learned is that you want VS Code (or gopls) to correctly identify a multi-module project. Please refer to this (your are using Go1.18).
And more about go modules.
And go workspace.
Hope those can help you.
Ok this is what I did:
First: Run go mod tidy as suggested by Mushroomator in my root project, and it didn't work
Second: remove the GOPATH environment variable, since as JimB commented it is no longer used, and it didn't work either.
Now, this is where I feel a bit confused, because maybe I did what they asked me to do but I don't know how to explain it.
So, it works and no longer shows a syntax error. Delete the GOPATH environment variable, SET GOMODULE111=on, and take out the Projects/Go directory where I have all the files and put it on the desktop and mark this syntax error:
Google
Todo
Imágenes
Noticias
Shopping
Vídeos
Más
Herramientas
Cerca de 1,260,000,000 resultados (0.39 segundos)
Español
Inglés
Now, as you can see, I open VS Code directly in the helloworld directory, and the syntax error disappears, but I wanted to have it open directly from the src directory to see everything I've learned so far:
I'm building AOSP from source and have created a small C++ program that prints some messages to logcat when started. Now I wanted to debug the program according to https://source.android.com/devices/tech/debug/gdb
In the reference you're encouraged to use lldb in favor of gdb and there is also a short section on using vs code as debugger.
However, I cannot find the mentioned script lldbclient anywhere in my source nor in Android Code Search, only gdbclient.py seems to be present.
Q1: Where can I find lldbclient script?
When running the gdbclient.py script the option --setup-forwarding vscode seems to be ignored and gdb is always started.
Q2: If there isn't a lldbclient script, what options do I have to pass to gdbclient.py to enable debugging with lldb and vs code?
What did I do so far?
gdbclient.py -r /data/mysample_bin --setup-forwarding vscode
Starts my native program with attached gdb and allows me to step through my program.
Though I do not know how to code python, I was able to track down a call sequence in the script to method generate_setup_script, which is called with parameter debugger=gdb. Therefore no lldb configuration for vs code is created. Passing --no-gdb or --lldb to the script doesn't change this behavior.
Q1: You can find lldbclient.py script in the repository https://android.googlesource.com/platform/development, branch android-s-beta-2 (or another android-s branch).
Q2: Android also provides some tutorial debugging with Vscode: https://source.android.com/devices/tech/debug/gdb#vscode.
I am using Juno package in an atom ide to debug the Julia code. How can I step into the function? The function is imported from a different file. I can execute a set of lines by selecting them and then pressing "ctrl+enter". Is there any short-cut for stepping into a function and execute line by line in the function. Thank you.
Is it possible to debug Julia code using gdb?
Thank you.
Historically there was Gallium.jl and it integrated nicely with Julia 0.6.
The full-blown debugger is still work-in-progress in Julia.
Now this is the best you can do.
What works:
the best bet today is probably to try Debugger.jl. Press ] for the package manager
add https://github.com/JuliaDebug/JuliaInterpreter.jl
add https://github.com/JuliaDebug/Debugger.jl
Once installed after using Debugger you can use #enter macro to control function flow.
Another option to try out is Rebugger.jl
The future:
There are two nice thread on Julia discourse explaining the current state:
Compiler work priorities
State of the debugger
I setup a breakpoint in my Go code with runtime.Breakpoint(), save the file (my editor, Atom with go-plus installed, runs go install . on save). Then I run Delve in terminal with dlv debug, and type continue after it starts.
When the breakpoint kicks in, I want to test a couple of things (basically to print reader's data via a bytes.Buffer). But, I get the following error
buf := new(bytes.Buffer): "1:5: expected 'EOF', found ':='"
and in general cannot do much more than print values.
Is it really not possible to do this sort of thing? I am used to Python's pdb where setting variables or calling functions is not a problem and I would expect Delve is capable of the same.
So, what am I doing wrong?
Not possible yet. Right now (2018-NOV) work is in progress on Delve, but unfinished.
Go runtime was changed recently to allow this kind of call. Delve have a Github issue tracking the progress of such feature, but is still experimental and incomplete.
Is there a way to run my .fs file with either a breakpoint or a command like System.Diagnostics.Debugger.Launch() in it so that I can use FSI to examine values, apply functions to them, plot them, etc.?
I know this question has been asked before but I tried the answers and could not make them work. Clear instructions or a link to a write-up explaining the process would be of great help not only to myself, but also, I believe, to all beginners.
Unfortunately, you cannot hit a breakpoint and jump into FSI. The context of a running .NET program is quite different to that of an interactive FSI session and they are not compatible enough to just switch between one or the other. I can understand an expectation of this kind of debugging when coming from other dynamic/interpreted languages such as JavaScript, Python etc. It is a really powerful feature.
As you have already noted, you can use the VS immediate window but you need to use its C#-like syntax and respect its limitations. Also, since it's not F#, you need to understand the F# to .NET conversion in order to make full use of it.
If you use Paket for dependency management in your project you have another option: Add generate_load_scripts: true to your paket.dependencies. This will give you a file like .paket\load\net452\main.group.fsx, which you can load into FSI to get all of the dependencies for your project. However, you are still responsible for loading in your own source files and building up some state similar to what is found at your desired breakpoint.
To hit a break point, in visual studio or visual studio code, you just click to the left of the line number you want to set your breakpoint. This is very much a supported feature in .fs files.