转到fmt.Scanln,它不回显用户键入的字符。 输入密码

How do I capture user input from the command line without echoing back the characters that the user types. I want to use this to capture a password. Like getpass.getpass in Python.

package main

import (
    "fmt"
)

func main() {
    var password string
    fmt.Scanln(&password)
}

There is no helper function in the standard library for this.

You have to create your own, or use an existing one like gopass (supports windows, unix, bsd).

Using gopass: (example taken from their website)

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

func main() {
    fmt.Printf("Password: ")
    pass := gopass.GetPasswd() // Silent, for *'s use gopass.GetPasswdMasked()
    // Do something with pass
}