From b2fee9a037effbf0006165abba99fb78d0ea8179 Mon Sep 17 00:00:00 2001 From: James Brown Date: Fri, 5 Aug 2016 16:50:09 -0700 Subject: [PATCH] detect IPv6 literals and wrap them in square brackets in GetDBUri --- go/mysql/connection.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/go/mysql/connection.go b/go/mysql/connection.go index 8ddedb4f6..01c1c9174 100644 --- a/go/mysql/connection.go +++ b/go/mysql/connection.go @@ -7,6 +7,7 @@ package mysql import ( "fmt" + "net" ) // ConnectionConfig is the minimal configuration required to connect to a MySQL server @@ -47,5 +48,11 @@ func (this *ConnectionConfig) Equals(other *ConnectionConfig) bool { } func (this *ConnectionConfig) GetDBUri(databaseName string) string { - return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", this.User, this.Password, this.Key.Hostname, this.Key.Port, databaseName) + var ip = net.ParseIP(this.Key.Hostname) + if (ip != nil) && (ip.To4() == nil) { + // Wrap IPv6 literals in square brackets + return fmt.Sprintf("%s:%s@tcp([%s]:%d)/%s", this.User, this.Password, this.Key.Hostname, this.Key.Port, databaseName) + } else { + return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", this.User, this.Password, this.Key.Hostname, this.Key.Port, databaseName) + } }