Skip to content

Commit 1712572

Browse files
committed
term: add examples for ReadPassword
These examples demonstrate how to use ReadPassword for interactive password input from the terminal. They highlight how to use the function, even if standard input is not a terminal (e.g. when it is a pipe). The example for Windows is especially interesting, because there seems to be a bug where CONIN$ cannot be used unless opened with the write flag.
1 parent a79de54 commit 1712572

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

read_password_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2021 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package term_test
6+
7+
import (
8+
"fmt"
9+
"os"
10+
11+
"golang.org/x/term"
12+
)
13+
14+
func ExampleReadPassword() {
15+
fmt.Print("Enter your password: ")
16+
p, err := term.ReadPassword(int(os.Stdin.Fd()))
17+
if err != nil {
18+
panic(err)
19+
}
20+
fmt.Printf("\nYou entered '%s'\n", string(p))
21+
}

read_password_unix_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2021 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// +build !windows
6+
7+
package term_test
8+
9+
import (
10+
"fmt"
11+
"os"
12+
13+
"golang.org/x/term"
14+
)
15+
16+
func ExampleReadPassword_unix() {
17+
// If standard input is not bound to the terminal, a password can
18+
// still be read from it.
19+
tty, err := os.Open("/dev/tty")
20+
if err != nil {
21+
panic(err)
22+
}
23+
defer tty.Close()
24+
fmt.Print("Enter your password: ")
25+
p, err := term.ReadPassword(int(tty.Fd()))
26+
if err != nil {
27+
panic(err)
28+
}
29+
fmt.Printf("\nYou entered '%s'\n", string(p))
30+
}

read_password_windows.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2021 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// +build windows
6+
7+
package term_test
8+
9+
import (
10+
"fmt"
11+
"os"
12+
13+
"golang.org/x/term"
14+
)
15+
16+
func ExampleReadPassword_windows() {
17+
// If standard input is not bound to the terminal, a password can
18+
// still be read from it. OpenFile must be used with the write flag
19+
// for CONIN$.
20+
tty, err := os.OpenFile("CONIN$", os.O_RDWR, 0)
21+
if err != nil {
22+
panic(err)
23+
}
24+
defer tty.Close()
25+
fmt.Print("Enter your password: ")
26+
p, err := term.ReadPassword(int(tty.Fd()))
27+
if err != nil {
28+
panic(err)
29+
}
30+
fmt.Printf("\nYou entered '%s'\n", string(p))
31+
}

0 commit comments

Comments
 (0)