stdin


Node.js

  1. const stdin = process.openStdin()
  2. process.stdout.write('Enter name: ')
  3. stdin.addListener('data', text => {
  4. const name = text.toString().trim()
  5. console.log('Your name is: ' + name)
  6. stdin.pause()
  7. })

Output

  1. Enter name: bob
  2. Your name is: bob

Go

  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. "strings"
  7. )
  8. func main() {
  9. reader := bufio.NewReader(os.Stdin)
  10. fmt.Print("Enter name: ")
  11. text, err := reader.ReadString('\n')
  12. if err != nil {
  13. panic(err)
  14. }
  15. name := strings.TrimSpace(text)
  16. fmt.Printf("Your name is: %s\n", name)
  17. }

Output

  1. Enter name: bob
  2. Your name is: bob