Search code examples
mysqlgolocalhostmysql-error-1045

golang editing previously set flag. MySQL error 1045


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!


Solution

  • 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).