Skip to content

Instantly share code, notes, and snippets.

@kaneshin
Last active July 4, 2016 14:39
Show Gist options
  • Save kaneshin/dc2a0316f91258856dd2a0ef9afa1751 to your computer and use it in GitHub Desktop.
Save kaneshin/dc2a0316f91258856dd2a0ef9afa1751 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"io/ioutil"
"os"
"syscall"
"golang.org/x/crypto/ssh/terminal"
)
func main() {
if terminal.IsTerminal(syscall.Stdin) {
// Execute: go run main.go
fmt.Print("Type something then press the enter key: ")
var stdin string
fmt.Scan(&stdin)
fmt.Printf("Result: %s\n", stdin)
return
}
// Execute: echo "foo" | go run main.go
body, err := ioutil.ReadAll(os.Stdin)
if err != nil {
panic(err)
}
fmt.Printf("Result: %s\n", string(body))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment