Graham King

Solvitas perambulum

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

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

package main

import (
	"fmt"
	"syscall"
	"golang.org/x/crypto/ssh/terminal"
)

func main() {
	if terminal.IsTerminal(syscall.Stdin) {
		fmt.Println("Yes is a terminal")
	} else {
		fmt.Println("No is not a terminal")
	}
}

For example:

$ go build myprog.go
$ ./myprog                # is a terminal
$ echo "boo" | ./myprog   # not a terminal

You might need to:

go get golang.org/x/crypto/ssh/terminal

Updated May 2012 to use the exp/terminal package.

Updated Dec 2014, terminal moved to crypto