January 3, 2012

In Go, is your terminal a console or a pipe? isatty golang

Posted in Software at 18:08 by graham

Is my terminal connected to the console, or to a pipe?

// Is given File a terminal?
func isatty(file *os.File) bool {
    stat, _ := file.Stat()
    return !stat.IsFifo()
}

if ! isatty(os.Stdin) {
  // read what was piped in
}

For example:

$ ./myprog                # isatty = yes
$ echo "boo" | ./myprog   # isatty = no

Mirrors the behaviour of Python’s isatty.