Last active
July 4, 2016 14:39
-
-
Save kaneshin/dc2a0316f91258856dd2a0ef9afa1751 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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