stdin
Node.js
const stdin = process.openStdin()
process.stdout.write('Enter name: ')
stdin.addListener('data', text => {
const name = text.toString().trim()
console.log('Your name is: ' + name)
stdin.pause()
})
Output
Enter name: bob
Your name is: bob
Go
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func main() {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter name: ")
text, err := reader.ReadString('\n')
if err != nil {
panic(err)
}
name := strings.TrimSpace(text)
fmt.Printf("Your name is: %s\n", name)
}
Output
Enter name: bob
Your name is: bob
当前内容版权归 miguelmota 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 miguelmota .