doutao5499
2015-02-02 08:49
浏览 140
已采纳

转到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)
}
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dstt1818 2015-02-02 08:55
    已采纳

    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
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题