爱意满满的作品展示区。
wangleineo

用 Python 写了一个 Golang 的 REPL 代替 Playground

  •  
  •   wangleineo ·
    RealHacker · Sep 13, 2015 · 2932 views
    This topic created in 3896 days ago, the information mentioned may be changed or developed.

    因为众所周知的原因, Go 官方的 Playground 比较慢;另外,用 Playground 来试验一点代码,也需要写 main 函数把语句包起来,感觉有点麻烦。就写了一个 Go 的 REPL ,跟 Python 的命令行比较像,用于尝试一些代码或者试用一些接口的时候感觉方便一些:

    Enter your Go statements Or command (start with !):
    >>> import "fmt"
    >>> import "strconv"
    >>> fmt.Println ("Hello, REPL")
    Hello, REPL
    >>> i := 42 
    >>> fmt.Println ("The meaning of life:", strconv.Itoa (i ))
    The meaning of life: 42
    >>> a := make ([]int, 3 )
    >>> for j:=1;j<=3;j++{
    ...     a=append (a, j*j )
    ... }
    ...
    >>> fmt.Println (a )
    [0 0 0 1 4 9]
    

    代码在这里:
    https://github.com/RealHacker/python-gems/blob/master/go_repl.py

    • 用的是 go 命令编译运行,所以 go 必须在 Path 里。
    • 虽然叫 REPL ,但是并不支持表达式的求值,输入的必须是完整的 Go 语句。虽然其实也可以支持,但用 Python 做 Go 表达式的判定感觉太麻烦。
    • 定义了变量,如果没使用,编译器会报错,这种情况还没有处理;如果 import 一个包,没有使用是没问题的。
    • 可以用!src 随时查看当前生成的代码,也支持删除之前输入的语句。
    • 没做过什么测试,一定有很多问题。

    欢迎大家踊跃拍砖。

    2 replies    2015-09-13 17:52:40 +08:00
    pynix
        1
    pynix  
       Sep 13, 2015
    貌似不错的演绎。。。
    pynix
        2
    pynix  
       Sep 13, 2015
    样子。。。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2720 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 15:55 · PVG 23:55 · LAX 08:55 · JFK 11:55
    ♥ Do have faith in what you're doing.