
Security News
The Changelog Podcast: Practical Steps to Stay Safe on npm
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.
Lightweight Golang REPL library, inspired by GNU Readline. You provide the Eval function, and go-repl does the rest.
Your REPLs that use this library will enjoy the following features:
EvalNotes:
Fetch this library with the following command:
$ go get -u github.com/openengineer/go-repl
In order to create your own REPL you have to define a type that implements the Handler interface:
type Handler interface {
Prompt() string
Tab(buffer string) string
Eval(line string) string
}
Here is a complete example (can also be found in ./examples/basic_repl.go):
package main
import (
"fmt"
"log"
"strconv"
"strings"
repl "github.com/openengineer/go-repl"
)
var helpMessage = `help display this message
add <int> <int> add two numbers
quit quit this program`
// implements repl.Handler interface
type MyHandler struct {
r *repl.Repl
}
func main() {
fmt.Println("Welcome, type \"help\" for more info")
h := &MyHandler{}
h.r = repl.NewRepl(h)
// start the terminal loop
if err := h.r.Loop(); err != nil {
log.Fatal(err)
}
}
func (h *MyHandler) Prompt() string {
return "> "
}
func (h *MyHandler) Tab(buffer string) string {
return "" // do nothing
}
func (h *MyHandler) Eval(line string) string {
fields := strings.Fields(line)
if len(fields) == 0 {
return ""
} else {
cmd, args := fields[0], fields[1:]
switch cmd {
case "help":
return helpMessage
case "add":
if len(args) != 2 {
return "\"add\" expects 2 args"
} else {
return add(args[0], args[1])
}
case "quit":
h.r.Quit()
return ""
default:
return fmt.Sprintf("unrecognized command \"%s\"", cmd)
}
}
}
func add(a_ string, b_ string) string {
a, err := strconv.Atoi(a_)
if err != nil {
return "first arg is not an integer"
}
b, err := strconv.Atoi(b_)
if err != nil {
return "second arg is not an integer"
}
return strconv.Itoa(a + b)
}
FAQs
Unknown package
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.

Security News
Ruby's creator Matz assumes control of RubyGems and Bundler repositories while former maintainers agree to step back and transfer all rights to end the dispute.