I use go-sql-driver/mysql to interact with the database.

For the connection, you must transfer the name / password / DB, as usual:

 ... db, err := sql.Open("mysql", "user:password@/db") ... 

Question: how correctly to transfer a name / password / bd not directly, but in the form of arguments?

The following option does not work:

 db, err := sql.Open("mysql", ("%s:%s@/%s", mysql_user, mysql_passwd, mysql_db)) 
  • one
    fmt.Sprintf ? - user227465
  • I'll tell you a terrible secret, the lines can be concatenated, put in simple language =) - Oma
  • @Oma just like in my favorite python, but I did not know :-) - approximatenumber

2 answers 2

 db, err := sql.Open("mysql", mysql_user+":"+mysql_passwd+"@/"+mysql_db) 
  • By the way, besides concatenation, is there another way? - approximatenumber

There is also a standard fmt library:

 import "fmt" //... db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@/%s", mysql_user, mysql_passwd, mysql_db))