How to make an interactive CLI tool in Golang

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.

Required packages

Sample code

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

How to make an interactive CLI tool in Golang
How to get help in an interactive shell
How to convert csv to tsv in CLI
How to make an embedded Linux device driver (11)
How to make an embedded Linux device driver (8)
How to make an embedded Linux device driver (1)
How to make an embedded Linux device driver (4)
How to make an embedded Linux device driver (7)
How to make an embedded Linux device driver (2)
How to make an embedded Linux device driver (3)
[Blender x Python] How to make an animation
How to make an embedded Linux device driver (6)
Make fixture an argument to parameterize in py.test
How to make an embedded Linux device driver (5)
How to make an embedded Linux device driver (10)
How to make Python Interpreter changes in Pycharm
How to make an embedded Linux device driver (9)
Tool to make mask image for ETC in Python
How to make Yubico Yubikey recognized in Manjaro Linux
Explain in detail how to make sounds with python
How to make an HTTPS server with Go / Gin
How to create an image uploader in Bottle (Python)
How to make an embedded Linux device driver (12) (Complete)
How to use Decorator in Django and how to make it
How to develop in Python
[Python] How to make an adjacency matrix / adjacency list [Graph theory]
How to swap elements in an array in Python, and how to reverse an array.
[Python] How to write an if statement in one sentence.
How to make a Python package (written for an intern)
How to manipulate the DOM in an iframe with Selenium
How to log in automatically like 1Password from the CLI
How to check if a value exists in an enum
How to get an overview of your data in Pandas
How to make an interactive LINE BOT 004 (answer the closing date of a listed company)
How to intentionally issue an error in the shell During testing
How to make a Japanese-English translation
[Python] How to do PCA in Python
How to handle session in SQLAlchemy
How to know the internal structure of an object in Python
How to write soberly in pandas
A game to go on an adventure in python interactive mode
How to use SQLite in Python
How to make a slack bot
How to create an email user
How to make a crawler --Advanced
How to make a recursive function
How to create a heatmap with an arbitrary domain in Python
How to convert 0.5 to 1056964608 in one shot
How to reflect CSS in Django
How to kill processes in bulk
How to use Mysql in python
Slack --APIGateway --Lambda (Python) --How to make a RedShift interactive app
[Django] How to read variables / constants defined in an external file
How to wrap C in Python
How to use ChemSpider in Python
How to make a deadman's switch
I tried to make an analysis base of 5 patterns in 3 years
[Blender] How to make a Blender plugin
How to use PubChem in Python
[Blender] How to make Blender scripts multilingual
[Python] Simple Japanese ⇒ I tried to make an English translation tool