getpasswd functionality in Go?

前端 未结 9 1693
春和景丽
春和景丽 2020-12-02 13:08

Situation:

I want to get a password entry from the stdin console - without echoing what the user types. Is there something com

9条回答
  •  囚心锁ツ
    2020-12-02 13:44

    I had a similar usecase and the following code snippet works well for me. Feel free to try this if you are still stuck here.

    import (
        "fmt"
        "golang.org/x/crypto/ssh/terminal"
    
    )
    
    func main() {
        fmt.Printf("Now, please type in the password (mandatory): ")
        password, _ := terminal.ReadPassword(0)
    
        fmt.Printf("Password is : %s", password)
    }
    

    Of course, you need to install terminal package using go get beforehand.

提交回复
热议问题