Home > Mobile >  How to parse URI gracefully using Go?
How to parse URI gracefully using Go?

Time:10-23

Here is my kafka connection information in etcd :

kafka://user:[email protected]:9092?mechanism=PLAIN&protocol=SASL_PLAINTEXT

When i get the information string from the etcd, i want to get the username user,password passwd and host 10.10.172.222:9092.

Now how can i parse Kafka connection information gracefully using Golang?

CodePudding user response:

Use net/url library

kafkaUrl := "kafka://[email protected]:9092?mechanism=PLAIN&protocol=SASL_PLAINTEXT"

u, err := url.Parse(kafkaUrl)
if err != nil {
    // handle error
}
user := u.User.Username()
pass, isPassSet := u.User.Password()
host := u.Host // host or host:port

hostname and port separetely

hostname := u.Hostname()
port := u.Port()
  • Related