Detect if a command is piped or not


Is there a way to detect if a command in go is piped or not?


cat test.txt | mygocommand #Piped, this is how it should be used
mygocommand # Not piped, this should be blocked

I’m reading from the Stdin reader := bufio.NewReader(os.Stdin).


You can do this using os.Stdin.Stat().

package main

import (

func main() {
    fi, _ := os.Stdin.Stat()

    if (fi.Mode() & os.ModeCharDevice) == 0 {
        fmt.Println("data is from pipe")
    } else {
        fmt.Println("data is from terminal")

(Adapted from

Answered By – user513951

Answer Checked By – Marilyn (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.