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();
});
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
Output
Enter name: bob
Your name is: bob
1
2
2
# 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)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Output
Enter name: bob
Your name is: bob
1
2
2
编辑 (opens new window)
上次更新: 2022/09/30, 11:34:22