Create an interactive CLI tool with golang.
When you hit the command, it waits for input and Loop infinitely until you enter the appropriate value.
package main
import (
"fmt"
"bufio"
"os"
)
func main() {
fmt.Println("Enter stop.")
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
if scanner.Text() == "stop" {
break
}
fmt.Println("Loops until stop is entered.")
}
fmt.Println("I'm done.")
}
//Waiting for input with this method
scanner.Scan()
//Returns true if the input value is successfully received
val := scanner.Scan()
fmt.Println(val)
// => true
//The entered value can be called by this method
scanner.Text()
Recommended Posts