[term] term: add examples for ReadPassword

1,024 views
Skip to first unread message

Gerrit Bot (Gerrit)

unread,
Jun 3, 2021, 1:08:12 PM6/3/21
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Gerrit Bot has uploaded this change for review.

View Change

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.

This contribution was suggested to me at https://github.com/golang/go/issues/46164.

Change-Id: Ib9bc595f57025f4df258d6724ea512f03894889f
GitHub-Last-Rev: 17125723315e87d2876e5781e956d67a5a21656b
GitHub-Pull-Request: golang/term#4
---
A read_password_test.go
A read_password_unix_test.go
A read_password_windows.go
3 files changed, 82 insertions(+), 0 deletions(-)

diff --git a/read_password_test.go b/read_password_test.go
new file mode 100644
index 0000000..b1eb3cd
--- /dev/null
+++ b/read_password_test.go
@@ -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))
+}
diff --git a/read_password_unix_test.go b/read_password_unix_test.go
new file mode 100644
index 0000000..82170bf
--- /dev/null
+++ b/read_password_unix_test.go
@@ -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))
+}
diff --git a/read_password_windows.go b/read_password_windows.go
new file mode 100644
index 0000000..a7bca3d
--- /dev/null
+++ b/read_password_windows.go
@@ -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))
+}

To view, visit change 324829. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: term
Gerrit-Branch: master
Gerrit-Change-Id: Ib9bc595f57025f4df258d6724ea512f03894889f
Gerrit-Change-Number: 324829
Gerrit-PatchSet: 1
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-MessageType: newchange

Gerrit Bot (Gerrit)

unread,
Jun 3, 2021, 3:59:39 PM6/3/21
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Gerrit Bot uploaded patch set #2 to this change.

View Change

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.

This contribution was suggested to me at https://github.com/golang/go/issues/46164.

Change-Id: Ib9bc595f57025f4df258d6724ea512f03894889f
GitHub-Last-Rev: b2824fc2bbc1d05a1bd8396cbbb3fc74aebfb4d9

GitHub-Pull-Request: golang/term#4
---
A read_password_test.go
A read_password_unix_test.go
A read_password_windows_test.go

3 files changed, 82 insertions(+), 0 deletions(-)

To view, visit change 324829. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: term
Gerrit-Branch: master
Gerrit-Change-Id: Ib9bc595f57025f4df258d6724ea512f03894889f
Gerrit-Change-Number: 324829
Gerrit-PatchSet: 2
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-MessageType: newpatchset

Richard Ulmer (Gerrit)

unread,
Jun 4, 2021, 11:20:22 AM6/4/21
to Gerrit Bot, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

View Change

1 comment:

To view, visit change 324829. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: term
Gerrit-Branch: master
Gerrit-Change-Id: Ib9bc595f57025f4df258d6724ea512f03894889f
Gerrit-Change-Number: 324829
Gerrit-PatchSet: 2
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-CC: Richard Ulmer <richar...@web.de>
Gerrit-Comment-Date: Fri, 04 Jun 2021 15:20:16 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Gerrit-MessageType: comment

TurkDevOps DevTurks-Team (Gerrit)

unread,
Jun 4, 2021, 12:01:30 PM6/4/21
to Gerrit Bot, goph...@pubsubhelper.golang.org, Richard Ulmer, golang-co...@googlegroups.com

Patch set 2:Code-Review +1

View Change

    To view, visit change 324829. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: term
    Gerrit-Branch: master
    Gerrit-Change-Id: Ib9bc595f57025f4df258d6724ea512f03894889f
    Gerrit-Change-Number: 324829
    Gerrit-PatchSet: 2
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-Reviewer: TurkDevOps DevTurks-Team <post.kad...@gmail.com>
    Gerrit-CC: Richard Ulmer <richar...@web.de>
    Gerrit-Comment-Date: Fri, 04 Jun 2021 16:01:24 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: Yes
    Gerrit-MessageType: comment

    Alex Brainman (Gerrit)

    unread,
    Jun 5, 2021, 3:43:11 AM6/5/21
    to Gerrit Bot, goph...@pubsubhelper.golang.org, Kadir Selçuk, Richard Ulmer, golang-co...@googlegroups.com

    Attention is currently required from: Kadir Selçuk , Richard Ulmer.

    View Change

    1 comment:

    • File read_password_windows_test.go:

      • Patch Set #2, Line 20: tty, err := os.OpenFile("CONIN$", os.O_RDWR, 0)

        I do not want to add this example, because this code was not designed to work. Therefore it can break any time in the future. And it would be impossible to write a test for it.

        If you disagree, then you you should submit proposal to document this behavior

        https://github.com/golang/proposal

    To view, visit change 324829. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: term
    Gerrit-Branch: master
    Gerrit-Change-Id: Ib9bc595f57025f4df258d6724ea512f03894889f
    Gerrit-Change-Number: 324829
    Gerrit-PatchSet: 2
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-Reviewer: Kadir Selçuk <post.kad...@gmail.com>
    Gerrit-CC: Alex Brainman <alex.b...@gmail.com>
    Gerrit-CC: Richard Ulmer <richar...@web.de>
    Gerrit-Attention: Kadir Selçuk <post.kad...@gmail.com>
    Gerrit-Attention: Richard Ulmer <richar...@web.de>
    Gerrit-Comment-Date: Sat, 05 Jun 2021 07:43:05 +0000

    Gerrit Bot (Gerrit)

    unread,
    Jun 11, 2021, 12:02:52 PM6/11/21
    to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

    Attention is currently required from: Kadir Selçuk , Richard Ulmer.

    Gerrit Bot uploaded patch set #3 to this change.

    View Change

    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.

    This contribution was suggested to me at https://github.com/golang/go/issues/46164.

    Change-Id: Ib9bc595f57025f4df258d6724ea512f03894889f
    GitHub-Last-Rev: 136cee71c27ed96f94f9b04834e593a537af210a

    GitHub-Pull-Request: golang/term#4
    ---
    A read_password_test.go
    A read_password_unix_test.go
    A read_password_windows_test.go
    3 files changed, 91 insertions(+), 0 deletions(-)

    To view, visit change 324829. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: term
    Gerrit-Branch: master
    Gerrit-Change-Id: Ib9bc595f57025f4df258d6724ea512f03894889f
    Gerrit-Change-Number: 324829
    Gerrit-PatchSet: 3
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-Reviewer: Kadir Selçuk <post.kad...@gmail.com>
    Gerrit-CC: Alex Brainman <alex.b...@gmail.com>
    Gerrit-CC: Richard Ulmer <richar...@web.de>
    Gerrit-Attention: Kadir Selçuk <post.kad...@gmail.com>
    Gerrit-Attention: Richard Ulmer <richar...@web.de>
    Gerrit-MessageType: newpatchset

    Laurent Demailly (Gerrit)

    unread,
    Jul 11, 2025, 8:48:16 PMJul 11
    to Gerrit Bot, goph...@pubsubhelper.golang.org, Alex Brainman, Kadir Selçuk, askassk braskassk, golang-co...@googlegroups.com
    Attention needed from Kadir Selçuk and askassk braskassk

    Laurent Demailly added 1 comment

    Patchset-level comments
    File-level comment, Patchset 3 (Latest):
    Laurent Demailly . resolved

    would be nice to merge this is still accurate

    Open in Gerrit

    Related details

    Attention is currently required from:
    • Kadir Selçuk
    • askassk braskassk
    Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement is not satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: term
    Gerrit-Branch: master
    Gerrit-Change-Id: Ib9bc595f57025f4df258d6724ea512f03894889f
    Gerrit-Change-Number: 324829
    Gerrit-PatchSet: 3
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-Reviewer: Kadir Selçuk <post.kad...@gmail.com>
    Gerrit-CC: Alex Brainman <alex.b...@gmail.com>
    Gerrit-CC: Laurent Demailly <ldem...@gmail.com>
    Gerrit-CC: askassk braskassk <qwp...@gmail.com>
    Gerrit-Attention: Kadir Selçuk <post.kad...@gmail.com>
    Gerrit-Attention: askassk braskassk <qwp...@gmail.com>
    Gerrit-Comment-Date: Sat, 12 Jul 2025 00:48:12 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    unsatisfied_requirement
    open
    diffy

    Alex Brainman (Gerrit)

    unread,
    Jul 27, 2025, 1:53:31 AMJul 27
    to Gerrit Bot, goph...@pubsubhelper.golang.org, Laurent Demailly, Kadir Selçuk, askassk braskassk, golang-co...@googlegroups.com
    Attention needed from Kadir Selçuk and askassk braskassk

    Alex Brainman voted Commit-Queue+1

    Commit-Queue+1
    Open in Gerrit

    Related details

    Attention is currently required from:
    • Kadir Selçuk
    • askassk braskassk
    Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement is not satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: term
    Gerrit-Branch: master
    Gerrit-Change-Id: Ib9bc595f57025f4df258d6724ea512f03894889f
    Gerrit-Change-Number: 324829
    Gerrit-PatchSet: 4
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-Reviewer: Alex Brainman <alex.b...@gmail.com>
    Gerrit-Reviewer: Kadir Selçuk <post.kad...@gmail.com>
    Gerrit-CC: Laurent Demailly <ldem...@gmail.com>
    Gerrit-CC: askassk braskassk <qwp...@gmail.com>
    Gerrit-Attention: Kadir Selçuk <post.kad...@gmail.com>
    Gerrit-Attention: askassk braskassk <qwp...@gmail.com>
    Gerrit-Comment-Date: Sun, 27 Jul 2025 05:53:21 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: Yes
    unsatisfied_requirement
    open
    diffy

    Alex Brainman (Gerrit)

    unread,
    Jul 27, 2025, 2:01:14 AMJul 27
    to Gerrit Bot, goph...@pubsubhelper.golang.org, Go LUCI, Laurent Demailly, Kadir Selçuk, askassk braskassk, golang-co...@googlegroups.com
    Attention needed from Kadir Selçuk , Laurent Demailly and askassk braskassk

    Alex Brainman added 1 comment

    Patchset-level comments
    Laurent Demailly . resolved

    would be nice to merge this is still accurate

    Alex Brainman

    Fair enough.

    I rebased this CL and run tests and they pass.

    If it looks good to you (since you are the user of this package), I will give +2 and hopefully we can merge this CL.

    Thank you.

    Open in Gerrit

    Related details

    Attention is currently required from:
    • Kadir Selçuk
    • Laurent Demailly
    • askassk braskassk
    Submit Requirements:
      • requirement is not satisfiedCode-Review
      • requirement is not satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedReview-Enforcement
      • requirement satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: comment
      Gerrit-Project: term
      Gerrit-Branch: master
      Gerrit-Change-Id: Ib9bc595f57025f4df258d6724ea512f03894889f
      Gerrit-Change-Number: 324829
      Gerrit-PatchSet: 4
      Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
      Gerrit-Reviewer: Alex Brainman <alex.b...@gmail.com>
      Gerrit-Reviewer: Kadir Selçuk <post.kad...@gmail.com>
      Gerrit-CC: Laurent Demailly <ldem...@gmail.com>
      Gerrit-CC: askassk braskassk <qwp...@gmail.com>
      Gerrit-Attention: Laurent Demailly <ldem...@gmail.com>
      Gerrit-Attention: Kadir Selçuk <post.kad...@gmail.com>
      Gerrit-Attention: askassk braskassk <qwp...@gmail.com>
      Gerrit-Comment-Date: Sun, 27 Jul 2025 06:01:05 +0000
      Gerrit-HasComments: Yes
      Gerrit-Has-Labels: No
      Comment-In-Reply-To: Laurent Demailly <ldem...@gmail.com>
      unsatisfied_requirement
      satisfied_requirement
      open
      diffy

      Laurent Demailly (Gerrit)

      unread,
      Sep 19, 2025, 7:01:10 PMSep 19
      to Gerrit Bot, goph...@pubsubhelper.golang.org, Go LUCI, Alex Brainman, Kadir Selçuk, askassk braskassk, golang-co...@googlegroups.com
      Attention needed from Kadir Selçuk and askassk braskassk

      Laurent Demailly added 2 comments

      File read_password_test.go
      Line 20, Patchset 4 (Latest): fmt.Printf("\nYou entered '%s'\n", string(p))
      Laurent Demailly . unresolved

      it's just a test/example but maybe %q instead of '%s' would be better as control characters are accepted (for better or worse) by ReadPassword

      File read_password_windows_test.go
      Line 20, Patchset 2: tty, err := os.OpenFile("CONIN$", os.O_RDWR, 0)
      Alex Brainman . unresolved

      I do not want to add this example, because this code was not designed to work. Therefore it can break any time in the future. And it would be impossible to write a test for it.

      If you disagree, then you you should submit proposal to document this behavior

      https://github.com/golang/proposal

      Laurent Demailly

      I actually agree this one is quite hairy/ugly - probably better as a stackoverflow "trick" than a godoc ? or we would need to hide away this as "windows.OpenConsoleIn()" or some such

      my bad for missing your comment earlier, I was going through the open PRs on github and it seems innocous

      Open in Gerrit

      Related details

      Attention is currently required from:
      • Kadir Selçuk
      • askassk braskassk
      Submit Requirements:
      • requirement is not satisfiedCode-Review
      • requirement is not satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedReview-Enforcement
      • requirement satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: comment
      Gerrit-Project: term
      Gerrit-Branch: master
      Gerrit-Change-Id: Ib9bc595f57025f4df258d6724ea512f03894889f
      Gerrit-Change-Number: 324829
      Gerrit-PatchSet: 4
      Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
      Gerrit-Reviewer: Alex Brainman <alex.b...@gmail.com>
      Gerrit-Reviewer: Kadir Selçuk <post.kad...@gmail.com>
      Gerrit-CC: Laurent Demailly <ldem...@gmail.com>
      Gerrit-CC: askassk braskassk <qwp...@gmail.com>
      Gerrit-Attention: askassk braskassk <qwp...@gmail.com>
      Gerrit-Attention: Kadir Selçuk <post.kad...@gmail.com>
      Gerrit-Comment-Date: Fri, 19 Sep 2025 23:01:03 +0000
      Gerrit-HasComments: Yes
      Gerrit-Has-Labels: No
      Comment-In-Reply-To: Alex Brainman <alex.b...@gmail.com>
      unsatisfied_requirement
      satisfied_requirement
      open
      diffy

      Alex Brainman (Gerrit)

      unread,
      Sep 22, 2025, 2:02:04 AMSep 22
      to Gerrit Bot, goph...@pubsubhelper.golang.org, Go LUCI, Laurent Demailly, Kadir Selçuk, askassk braskassk, golang-co...@googlegroups.com
      Attention needed from Kadir Selçuk and askassk braskassk

      Alex Brainman added 2 comments

      File read_password_test.go
      Line 20, Patchset 4 (Latest): fmt.Printf("\nYou entered '%s'\n", string(p))
      Laurent Demailly . unresolved

      it's just a test/example but maybe %q instead of '%s' would be better as control characters are accepted (for better or worse) by ReadPassword

      Alex Brainman

      @ldem...@gmail.com this CL is more than 4 years old. I doubt the CL authors would be there to address your comments.

      File read_password_windows_test.go
      Line 20, Patchset 2: tty, err := os.OpenFile("CONIN$", os.O_RDWR, 0)
      Alex Brainman . unresolved

      I do not want to add this example, because this code was not designed to work. Therefore it can break any time in the future. And it would be impossible to write a test for it.

      If you disagree, then you you should submit proposal to document this behavior

      https://github.com/golang/proposal

      Laurent Demailly

      I actually agree this one is quite hairy/ugly - probably better as a stackoverflow "trick" than a godoc ? or we would need to hide away this as "windows.OpenConsoleIn()" or some such

      my bad for missing your comment earlier, I was going through the open PRs on github and it seems innocous

      Alex Brainman

      I actually agree this one is quite hairy/ugly - probably better as a stackoverflow "trick" than a godoc ? or we would need to hide away this as "windows.OpenConsoleIn()" or some such

      Like I said earlier it is your call, if we should submit this. I am unfamiliar with this code and have no time to investigate it properly.


      my bad for missing your comment earlier, ...

      No problem.

      Gerrit-Comment-Date: Mon, 22 Sep 2025 06:01:53 +0000
      Gerrit-HasComments: Yes
      Gerrit-Has-Labels: No
      Comment-In-Reply-To: Alex Brainman <alex.b...@gmail.com>
      Comment-In-Reply-To: Laurent Demailly <ldem...@gmail.com>
      unsatisfied_requirement
      satisfied_requirement
      open
      diffy
      Reply all
      Reply to author
      Forward
      0 new messages