Returning the resulting output and defining best practices
When returning output from a process, it’s important to know to who or what you’re returning data. It’s incredibly important to return output that’s human-readable. However, to determine whether you’re returning data to a human or a machine, check whether you’re writing to a TTY. Remember TTY? You can refer to Chapter 1, Understanding CLI Standards, in which we discussed the history of the CLI interface and the term TTY, short for teletypewriter or teletype.
If writing to a TTY, we can check whether the stdout
file descriptor refers to a terminal or not, and change the output depending on the result.
Let’s check out this block of code, which checks whether the stdout
file descriptor is writing to a TTY or not:
if fileInfo, _ := os.Stdout.Stat(); (fileInfo.Mode() & os.ModeCharDevice) != 0 { fmt.Println("terminal...