Golang编辑先前设置的标志。MySQL错误1045

huangapple go评论72阅读模式
英文:

golang editing previously set flag. MySQL error 1045

问题

我在用户输入后设置一个标志来作为连接参数用于MySQL数据库,但是遇到了问题。以下是我的代码片段:

func init() {
    flag.StringVar(&flagUser, "user", "root", "User")

    reader := bufio.NewReader(os.Stdin)
    fmt.Print("Enter username: ")
    inputUser, _ := reader.ReadString('\n')

    f := flag.Lookup("user")
    if inputUser != f.Value.String() {
        flag.StringVar(&flagUser, "user", inputUser, "User")
    }

    flag.Parse()
}

即使我只调用一次flag.Parse,并且用户输入也是"root",我仍然得到一个1045错误("mysql access denied for user root@localhost"),如果我不编辑标志就不会出现这个错误。我非常感谢对这个问题的任何建议。

再次感谢你的帮助!

英文:

I'm having trouble setting a flag after user input to use as a connection parameter to a MySQL Database. Below is a snippet of my code:

func init() {

    flag.StringVar(&flagUser, "user", "root", "User")
	
	reader := bufio.NewReader(os.Stdin)
	fmt.Print("Enter username: ")
	inputUser, _ := reader.ReadString('\n')
		
	f := flag.Lookup("user")
		if inputUser != f.Value.String() {
			flag.StringVar(&flagUser, "user", inputUser, "User")
		}
	
	flag.Parse()
}

Even if I call flag.Parse only once and the user input is also "root", i get a 1045 ("mysql access denied for user root@localhost"), which doesn't occur if i don't edit the flag. I would really appreciate any suggestions on this issue.

Once again, thanks for the help!

答案1

得分: 0

在进行了更多的研究后,我发现没有必要通过用户输入来编辑指定连接参数的标志,因为在运行Go应用程序时可以传递这些MySQL参数(例如:-user,-p等)。

英文:

After doing some more research I've found that there's no need to edit a flag specifying a connection parameter through user input, since it is possible to pass such mysql parameters when running a go application (i.e. : -user, -p, etc).

huangapple
  • 本文由 发表于 2015年1月30日 00:33:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/28219810.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定