Help us shape the future of Go! Take the 2026 Go Developer Survey

prompter

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 19, 2026 License: MIT Imports: 8 Imported by: 244

README

prompter

Test Status Coverage Status MIT License GoDev

Description

utility for easy prompting in Golang

Synopsis

twitterID := prompter.Prompt("Enter your twitter ID", "")
lang := prompter.Choose("Which language do you like the most?", []string{"Perl", "Golang", "Scala", "Ruby"}, "Perl")
passwd := prompter.Password("Enter your password")
var likeSushi bool = prompter.YN("Do you like sushi?", true)
var likeBeer bool = prompter.YesNo("Do you like beer?", false)

Features

  • Easy to use
  • Prompts and validation messages go to stderr, keeping stdout clean
  • Care non-interactive (not a tty) environment
    • Default is used and the process is not blocked
  • No howeyc/gopass (which uses cgo) dependency
    • cross build friendly
  • Customizable prompt setting by using &prompter.Prompter{} directly

Read errors

Use Prompter.PromptWithError() to distinguish an answer from EOF or a read error:

answer, err := (&prompter.Prompter{
    Message: "Enter your name",
    Default: "guest",
}).PromptWithError()
if err != nil {
    return err
}
fmt.Println(answer)

An empty line uses Default, but EOF and read errors return an empty answer and an error. Prompt() and the convenience functions instead report read errors to stderr and return an empty answer (false for YN and YesNo). Explicit default mode and non-interactive mode still return Default without reading input.

License

MIT

Author

Songmu

Documentation

Overview

Package prompter is utility for easy prompting

Index

Examples

Constants

View Source
const VERSION = "1.0.0"

VERSION version of prompter

Variables

This section is empty.

Functions

func Choose

func Choose(message string, choices []string, defaultChoice string) string

Choose make a choice

Example
package main

import (
	"fmt"

	"github.com/Songmu/prompter"
)

func main() {
	lang := prompter.Choose("Which language do you like the most?", []string{"Perl", "Golang", "Scala", "Ruby"}, "Perl")
	fmt.Println("Perl")
	fmt.Printf("Great! You like %s!", lang)
}
Output:
Perl
Great! You like Perl!

func Password

func Password(message string) string

Password asks password

Example
package main

import (
	"fmt"

	"github.com/Songmu/prompter"
)

func main() {
	passwd := prompter.Password("Enter your password")
	_ = passwd
	fmt.Println("****")
	fmt.Print("I got your password! :P")
}
Output:
****
I got your password! :P

func Prompt

func Prompt(message, defaultAnswer string) string

Prompt simple prompting

Example
package main

import (
	"fmt"

	"github.com/Songmu/prompter"
)

func main() {
	answer := prompter.Prompt("Enter your twitter ID", "")
	_ = answer
	fmt.Println("Songmu")
	fmt.Printf("Hi Songmu!")
}
Output:
Songmu
Hi Songmu!

func Regexp

func Regexp(message string, reg *regexp.Regexp, defaultAnswer string) string

Regexp checks the answer by regexp

func YN

func YN(message string, defaultToYes bool) bool

YN y/n choice

Example
package main

import (
	"fmt"

	"github.com/Songmu/prompter"
)

func main() {
	if prompter.YN("Do you like sushi?", true) {
		fmt.Println("y")
		fmt.Print("Nice! Let's go sushi bar!")
	}
}
Output:
y
Nice! Let's go sushi bar!

func YesNo

func YesNo(message string, defaultToYes bool) bool

YesNo yes/no choice

Example
package main

import (
	"fmt"

	"github.com/Songmu/prompter"
)

func main() {
	if !prompter.YesNo("Do you like beer?", false) {
		fmt.Println("no")
		fmt.Print("Oops!")
	}
}
Output:
no
Oops!

Types

type Prompter

type Prompter struct {
	Message string
	// choices of answer
	Choices    []string
	IgnoreCase bool
	Default    string
	// specify answer pattern by regexp. When both Choices and Regexp are specified, Regexp takes a priority.
	Regexp *regexp.Regexp
	// for passwords and so on.
	NoEcho     bool
	UseDefault bool
	// contains filtered or unexported fields
}

Prompter is object for prompting

Example
package main

import (
	"fmt"

	"github.com/Songmu/prompter"
)

func main() {
	input := (&prompter.Prompter{
		Choices:    []string{"aa", "bb", "cc"},
		Default:    "aa",
		Message:    "please select",
		IgnoreCase: true,
	}).Prompt()
	fmt.Println("aa")
	fmt.Printf("your input is %s.", input)
}
Output:
aa
your input is aa.

func (*Prompter) Prompt

func (p *Prompter) Prompt() string

Prompt displays a prompt and returns the answer. On a read error, it reports the error to stderr and returns an empty string. Use PromptWithError to handle read errors directly.

func (*Prompter) PromptWithError added in v1.0.0

func (p *Prompter) PromptWithError() (string, error)

PromptWithError displays a prompt and returns the answer or a read error. EOF and read errors are not treated as empty answers or replaced by Default.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL