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?

package main

import (

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

Leave a Comment

Note: Your comment will only appear on the site once I approve it manually. This can take a day or two. Thanks for taking the time to comment.