go-input: github.com/tcnksm/go-input Index | Examples | Files | Directories

package input

import "github.com/tcnksm/go-input"

Package input reads user input at the console. http://github.com/tcnksm/go-input

ui := &input.UI{
    Writer: os.Stdout,
    Reader: os.Stdin,
}

query := "What is your name?"
name, err := ui.Ask(query, &input.Options{
    Default: "tcnksm",
    Required: true,
    Loop:     true,
})

Index

Examples

Package Files

ask.go input.go read.go read_unix.go select.go

Constants

const LineSep = "\n"

LineSep is the separator for windows or unix systems

Variables

var (
    // Errs are error returned by input functions.
    // It's useful for handling error from outside of input functions.
    ErrEmpty       = errors.New("default value is not provided but input is empty")
    ErrNotNumber   = errors.New("input must be number")
    ErrOutOfRange  = errors.New("input is out of range")
    ErrInterrupted = errors.New("interrupted")
)

type Options Uses

type Options struct {
    // Default is the default value which is used when no thing
    // is input.
    Default string

    // Loop loops asking user to input until getting valid input.
    Loop bool

    // Required returns error when input is empty.
    Required bool

    // HideDefault hides default var output.
    HideDefault bool

    // HideOrder hides order comment ('Enter a value')
    HideOrder bool

    // Hide hides user input is prompting console.
    Hide bool

    // Mask hides user input and will be matched by MaskVal
    // on the screen. By default, MaskVal is asterisk(*).
    Mask bool

    // MaskDefault hides default value. By default, MaskVal is asterisk(*).
    MaskDefault bool

    // MaskVal is a value which is used for masking user input.
    // By default, MaskVal is asterisk(*).
    MaskVal string

    // ValidateFunc is function to do extra validation of user
    // input string. By default, it does nothing (just returns nil).
    ValidateFunc ValidateFunc
}

Options is structure contains option for input functions.

type UI Uses

type UI struct {
    // Writer is where output is written. For example a query
    // to the user will be written here. By default, it's os.Stdout.
    Writer io.Writer

    // Reader is source of input. By default, it's os.Stdin.
    Reader io.Reader
    // contains filtered or unexported fields
}

UI is user-interface of input and output.

func DefaultUI Uses

func DefaultUI() *UI

DefaultUI returns default UI. It outputs to stdout and intputs from stdin.

func (*UI) Ask Uses

func (i *UI) Ask(query string, opts *Options) (string, error)

Ask asks the user for input using the given query. The response is returned as string. Error is returned based on the given option. If Loop is true, it continue to ask until it receives valid input.

If the user sends SIGINT (Ctrl+C) while reading input, it catches it and return it as a error.

Code:

ui := &UI{
    // In real world, Reader is os.Stdin and input comes
    // from user actual input.
    Reader: bytes.NewBufferString("tcnksm"),
    Writer: ioutil.Discard,
}

query := "What is your name?"
name, _ := ui.Ask(query, &Options{})

fmt.Println(name)

Output:

tcnksm

func (*UI) Select Uses

func (i *UI) Select(query string, list []string, opts *Options) (string, error)

Select asks the user to select a item from the given list by the number. It shows the given query and list to user. The response is returned as string from the list. By default, it checks the input is the number and is not out of range of the list and if not returns error. If Loop is true, it continue to ask until it receives valid input.

If the user sends SIGINT (Ctrl+C) while reading input, it catches it and return it as a error.

Code:

ui := &UI{
    // In real world, Reader is os.Stdin and input comes
    // from user actual input.
    Reader: bytes.NewBufferString("3\n"),
    Writer: ioutil.Discard,
}

query := "Which language do you prefer to use?"
lang, _ := ui.Select(query, []string{"go", "Go", "golang"}, &Options{
    Default: "Go",
})

fmt.Println(lang)

Output:

golang

type ValidateFunc Uses

type ValidateFunc func(string) error

ValidateFunc is function to validate the user input.

The following example shows validating the user input is 'Y' or 'n' when asking yes or no question.

Code:

ui := &UI{
    // In real world, Reader is os.Stdin and input comes
    // from user actual input
    Reader: bytes.NewBufferString("Y\n"),
    Writer: ioutil.Discard,
}

query := "Do you love golang [Y/n]"
ans, _ := ui.Ask(query, &Options{
    // Define validateFunc to validate user input is
    // 'Y' or 'n'. If not returns error.
    ValidateFunc: func(s string) error {
        if s != "Y" && s != "n" {
            return fmt.Errorf("input must be Y or n")
        }

        return nil
    },
})

fmt.Println(ans)

Output:

Y

Directories

PathSynopsis
_example

Package input imports 11 packages (graph) and is imported by 42 packages. Updated 2018-08-22. Refresh now. Tools for package owners.