-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
3 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright 2021 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package term_test | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"golang.org/x/term" | ||
) | ||
|
||
func ExampleReadPassword() { | ||
fmt.Print("Enter your password: ") | ||
p, err := term.ReadPassword(int(os.Stdin.Fd())) | ||
if err != nil { | ||
panic(err) | ||
} | ||
fmt.Printf("\nYou entered '%s'\n", string(p)) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright 2021 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
// +build !windows | ||
|
||
package term_test | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"golang.org/x/term" | ||
) | ||
|
||
func ExampleReadPassword_unix() { | ||
// If standard input is not bound to the terminal, a password can | ||
// still be read from it. | ||
tty, err := os.Open("/dev/tty") | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer tty.Close() | ||
fmt.Print("Enter your password: ") | ||
p, err := term.ReadPassword(int(tty.Fd())) | ||
if err != nil { | ||
panic(err) | ||
} | ||
fmt.Printf("\nYou entered '%s'\n", string(p)) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright 2021 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
// +build windows | ||
|
||
package term_test | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"golang.org/x/term" | ||
) | ||
|
||
func ExampleReadPassword_windows() { | ||
// If standard input is not bound to the terminal, a password can | ||
// still be read from it. OpenFile must be used with the write flag | ||
// for CONIN$. | ||
tty, err := os.OpenFile("CONIN$", os.O_RDWR, 0) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer tty.Close() | ||
fmt.Print("Enter your password: ") | ||
p, err := term.ReadPassword(int(tty.Fd())) | ||
if err != nil { | ||
panic(err) | ||
} | ||
fmt.Printf("\nYou entered '%s'\n", string(p)) | ||
} |