-
Notifications
You must be signed in to change notification settings - Fork 148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Printed output is stacking, is this intended behavior? #67
Comments
I'm having the same. Quite annoying really.. |
[NOTE] you can see current source code by |
I think the issue here is that most people using a REPL don't expect every previous line to be re-run every time. The expectation is that the effects results of running previous lines (variable declarations/manipulation, etc.) will be kept, but not that the lines will actually be run again every time (with possible side effects). For example, I created a small server that logs the URL being accessed for each request. Input to gore> :import "net/http"
gore> http.Get("http://localhost:8081/1")
gore> http.Get("http://localhost:8081/2")
gore> http.Get("http://localhost:8081/3")
gore> http.Get("http://localhost:8081/4")
gore> http.Get("http://localhost:8081/5")
gore> http.Get("http://localhost:8081/6")
gore> http.Get("http://localhost:8081/7")
gore> http.Get("http://localhost:8081/8") Output from server:
Whereas the expected output from the server would be:
Using the Node REPL (for example) works as expected. Input: > http = require("http")
> http.get("http://localhost:8081/1")
> http.get("http://localhost:8081/2")
> http.get("http://localhost:8081/3")
> http.get("http://localhost:8081/4")
> http.get("http://localhost:8081/5")
> http.get("http://localhost:8081/6")
> http.get("http://localhost:8081/7")
> http.get("http://localhost:8081/8") Server output:
|
As for fmt.Print, monkey patching library can be the saver but generally speaking we have to consider implementing a real interpreter which will cost much as reimplementation of golang compiler. Since swift has a repl based on llvm (since swift compiler itself is based on llvm, apparently), can we use llgo? package main
import (
"fmt"
"io/ioutil"
"os"
"reflect"
"bou.ke/monkey"
)
func patchWrite() *monkey.PatchGuard {
var guard *monkey.PatchGuard
guard = monkey.PatchInstanceMethod(
reflect.TypeOf(os.Stdout), "Write",
func(f *os.File, b []uint8) (int, error) {
if f.Fd() == 1 || f.Fd() == 2 {
return len(b), nil
}
guard.Unpatch()
defer guard.Restore()
return f.Write(b)
},
)
return guard
}
func main() {
writeGuard := patchWrite()
x, err := fmt.Println(10)
fmt.Printf("%d\n", 10)
fmt.Fprintf(os.Stderr, "foo")
// Should writing to a file be ignored or not?
ioutil.WriteFile("/tmp/test.txt", []byte("hello"), os.FileMode(0600))
writeGuard.Unpatch()
fmt.Printf("%+v %+v\n", x, err)
fmt.Println(20)
} |
This is unlikely to solve in this project so closing. There's another tool (yaegi) which solved this problem with much work in interpreter. |
See the above short session, and note that the calls to
fmt.Println("ha!")
seem to stack and repeat each timefmt.Println
is used, is this intended?Also note that omitting the semicolon results in
being printed after the accumulated output from
fmt.Println
.The text was updated successfully, but these errors were encountered: