doutao5499
doutao5499
2015-02-02 08:49

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

  • IT行业问题
  • 计算机技术
  • it技术
  • 编程语言问答
  • 互联网问答
已采纳

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 dstt1818 6年前

    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
    }
    
    点赞 评论 复制链接分享

为你推荐