Skip to content

Instantly share code, notes, and snippets.

@prashant-shahi
Created April 24, 2024 14:04
Show Gist options
  • Save prashant-shahi/f61dfc882f96022b5ae21c4fa12b64ae to your computer and use it in GitHub Desktop.
Save prashant-shahi/f61dfc882f96022b5ae21c4fa12b64ae to your computer and use it in GitHub Desktop.
Example clickhouse DNS strings and how it is parsed
package main
import (
"fmt"
"net/url"
"strings"
)
func main() {
dsnStrings := []string{
"tcp://user1:usr-pwd1@clickhouse:9000/sn_traces",
"tcp://user2:usr-pwd2@clickhouse:9000/sn_metrics",
"tcp://user3:usr-pwd3@clickhouse:9000/sn_logs",
}
for _, dsnString := range dsnStrings {
dsn, err := url.Parse(dsnString)
if err != nil {
panic(err)
}
fmt.Printf("dsn: %s\n", dsnString)
fmt.Printf("scheme: %s\n", dsn.Scheme)
fmt.Printf("host: %s\n", dsn.Host)
fmt.Printf("path: %s\n", dsn.Path)
db := strings.TrimPrefix(dsn.Path, "/")
fmt.Printf("database: %s\n", db)
fmt.Printf("username: %s\n", dsn.User.Username())
if passwd, ok := dsn.User.Password(); ok {
fmt.Printf("password: %s\n\n", passwd)
} else {
fmt.Printf("password: <empty>\n\n")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment