2

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?

1 Answer 1

5

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() 
Sign up to request clarification or add additional context in comments.

3 Comments

Done! Thanks! (by the way, u.Host can get the host or host:port
Actually both it is just string so if port is provided it will be there if not... this is from source code see comment Host string // host or host:port
If you want to get hostname and port separatelly use u.Hostname() and u.Port()

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.