3 ms·
> The problem is that I can't declare the variable and set it's value as part of the same operation. Care to elaborate? Consider the following: var foo =
by randomdata 9y ago
> The problem is that I can't declare the variable and set it's value as part of the same operation.
Care to elaborate? Consider the following:
var foo = func() struct{ Bar int } {
if os.Getenv("BAR") == "1" {
return struct{ Bar int }{1}
}
return struct{ Bar int }{2}
}()
Is there a case where the foo variable can be assigned to something other than one of the two conditional-dependent values?
- vec 9y agoOkay, "can't" is too strong a word. But I don't know a good word for "it's technically possible but is cumbersome, is not idiomatic, and requires introducing another stack frame for no good reason".
- randomdata 9y ago> and requires introducing another stack frame for no good reason With a given implementation or is there something in the Go specification that prevents this from being compiled away? If I were to write something similar in, say, C++ with a good compiler I would expect the generated code to be equivalent to using a ternary operator.