A more modern implementation shouldn't need to use unused imports like the other answers have been suggesting. According to https://cloud.google.com/sql/docs/mysql/connect-app-engine-standard#go_1, you should be using something like this:
import (
"cloud.google.com/go/cloudsqlconn"
"github.com/go-sql-driver/mysql"
)
func connectWithConnector() (*sql.DB, error) {
d, err := cloudsqlconn.NewDialer(context.Background(), cloudsqlconn.WithLazyRefresh())
if err != nil {
return nil, fmt.Errorf("cloudsqlconn.NewDialer: %w", err)
}
var opts []cloudsqlconn.DialOption
if env.usePrivate != "" {
opts = append(opts, cloudsqlconn.WithPrivateIP())
}
mysql.RegisterDialContext("cloudsqlconn",
func(ctx context.Context, addr string) (net.Conn, error) {
return d.Dial(ctx, env.instanceConnectionName, opts...)
})
dbPool, err := sql.Open("mysql", dbConnString)
if err != nil {
return nil, fmt.Errorf("sql.Open: %w", err)
}
return dbPool, nil
}
Make sure that your network name (in the connection string) matches the one passed to mysql.RegisterDialContext. Additionally, your instance name should be the one listed on https://console.cloud.google.com/sql (of the form project:region:instance-id).
_ "github.com/go-sql-driver/mysql"? It shouldn't give imported and not used when you put underscore before package name.