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 (

func main() {
	if term.IsTerminal(int(os.Stdin.Fd())) {
		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

Updated May 2012 to use the exp/terminal package.

Updated Dec 2014, terminal moved to crypto

Updated Aug 2023, terminal moved to x/term. Thanks Ewen!