编写CLI:如何避免在屏幕上显示键入的密码[重复]

This question already has an answer here:

I'm writing a command line tool with Go and one of the command will query the user for its username and password (and save it to a config file inside the home directory).

For now I couldn't realize how to replace the typed password to '*' or even not to type anything as a lot of command line tools are doing.

How does can this be done when using golang?

</div>

Type this stuff into Google before you come here.

you can do this by execing stty -echo to turn off echo and then stty echo after reading in the password to turn it back on

OR

Just saw a mail in #go-nuts maillist. There is someone who wrote quite a simple go package to be used. You can find it here: https://github.com/howeyc/gopass

It something like that:

package main

import "fmt" import "github.com/howeyc/gopass"

func main() {
    fmt.Printf("Password: ")
    pass := gopass.GetPasswd()
    // Do something with pass }

Taken from another Stackoverflow post.