3 ms·
When you write: cli_host := os.Getenv("STOMP_HOST") if cli_host != "" { client.Host = cli_host } you would be more idiomatic if you use a ; like thi
by jgrahamc 14y ago
When you write:
cli_host := os.Getenv("STOMP_HOST")
if cli_host != "" {
client.Host = cli_host
}
you would be more idiomatic if you use a ; like this:
if cli_host := os.Getenv("STOMP_HOST"); cli_host != "" {
client.Host = cli_host
}
But given how repetitive your setOpts is I think I might have rewritten it something like this:
func getEnv(e string, d string) (r string) {
if r = os.Getenv(e); r == "" {
r = d
}
return
}
func (client *Client) setOpts() {
client.Host = getEnv("STOMP_HOST", "localhost")
client.Port = getEnv("STOMP_PORT", "62613")
client.User = getEnv("STOMP_USER", "")
client.Password = getEnv("STOMP_PASSWORD", "")
client.Uuid = getEnv("STOMP_UUID", stompngo.Uuid())
}
But then I have a thing about duplication.
Have fun with Go!
- robotmay 14y agoAh ha, I'd seen that style being used in a couple of libraries but I wasn't sure which was the more accepted standard. I'll make sure I use that format in the future. Update for your update: Aye, I wouldn't normally write so repetitively but I wasn't too sure about the best practice for things like that and I decided to just hash it out so I could play with the data. Thanks for the tips; I'm really keen to improve quickly with Go, as it's very enjoyable.
- james4k 14y agoI've always had a bit of an issue with these if statements. It might be slightly more concise, but your code also isn't as straightforward to read. In fact, it's really only more concise in its use of vertical space, so for clarity's sake I'm not sure I see the point.