3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

DEPARTUREAdvent Calendar 2023

Day 4

色々な言語で簡単なREPLを実装する #golang

Last updated at Posted at 2023-12-03

続きです。

REPLとは

こちらの記事を参照してください。

Goで実装する

main.go

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	scanner := bufio.NewScanner(os.Stdin)
cmd:
	for {
		fmt.Print("go> ")
		if !scanner.Scan() {
			break
		}
		prompt := scanner.Text()
		switch prompt {
		case "exit":
			fmt.Println("Bye!")
			break cmd
		default:
			fmt.Println(prompt)
		}
	}
}

実行する

$ go mod edit -module=example.com/app
$ go build
$ ./app
go> 1 + 1
1 + 1
go> foobar
foobar
go> baz
baz
go> exit
Bye!

CTRL-CやCTRL-Dの処理を実装しておけばよかったかも。

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?