4 ms·
Am I the only one who found the described behavior to be intuitively correct? I did expect it to print "abc.com".
by noisy_boy 1y ago
Am I the only one who found the described behavior to be intuitively correct? I did expect it to print "abc.com".
- _2d30 1y agoI don’t write Go at all but given the first example, also expected this. I was very surprised that either example compiled, though.
- adastra22 1y agoWhat is the intuition for that?
- b_e_n_t_o_n 1y agoYeah it makes sense to me as well.
- nine_k 1y agoThis may be intuitively correct, but to my mind it is architecturally wrong. A good language should not tolerate ambiguity and offer to guess which behavior is correct.
- tayo42 1y agoWhy is it ambiguous though? The second URL is nested Are thy not accessed like opts.URL == abc.com and opts.BarConnectionOptions.URL == xyz.com what leads you think otherwise?
- tsimionescu 1y agoIf there were no duplicate URL field, say they were called FooURL and BarcoURL, you could access them as `opts.FooURL` and `opts.BarcoURL`. They are both fields of `opts`, via embedding. It's just that FooURL is embedded directly, while BarcoURL is coming from two levels of embedding.
- wild_egg 1y agoIt's not ambiguous though. The behaviour is very clearly defined in the language spec. https://go.dev/ref/spec#Selectors https://go.dev/ref/spec#Selectors As far as language specs go, Go's is really quite concise and I strongly encourage everyone I onboard to spend an afternoon reading it end to end at some point in their first couple weeks.
- nvlled 1y agoI'm confused at the responses saying this intuitive. It's like saying: var x string x = "abc.com" x = "xyz.com" fmt.Println(x) will print abc.com and that's totally expected. The normal intuition would be that the latter operations or (re)definitions override the preceding ones.
- graynk 1y agoIt's not at all like saying that. The order of operations has nothing to do with it. opts := Options{ FooService: FooService{URL: "abc.com"}, BarService: BarService{ BarConnectionOptions: BarConnectionOptions{ URL: "xyz.com", }, }, } is equivalent to opts := Options{ BarService: BarService{ BarConnectionOptions: BarConnectionOptions{ URL: "xyz.com", }, }, FooService: FooService{URL: "abc.com"}, }
- nvlled 1y agoI'm making an analogy using simple assignments to show more clearly that it's actually counter-intuitive, I'm not claiming what you seem to be refuting, which at best seems to be beside the point.
- graynk 1y agoAnd the analogy is invalid, because it completely replaces the intuitive thing that's happening with an unintuitive thing that isn't happening. There are no "latter operations or (re)definitions" here.
- nvlled 1y ago> There are no "latter operations or (re)definitions" here. Yes, in the literal narrow sense, there is no such thing in the submitted article (if it isn't already clear, I'm referring to my own example). That's why it's an analogy. I don't know the precise term that go uses for this, closest is probably "shadowing", but again it doesn't matter, it is besides the point. The point is that the exhibited behaviour is unintuitive, in contrast to what the others are saying. > it completely replaces the intuitive thing that's happening with an unintuitive thing that isn't happening What is the intuitive thing are you referring to here? If it's my example, then you are in total agreement with me, but you seem to think otherwise. If you are referring to the linked article, then you are just merely invoking tautology, to disagree with me. It's intuitive because you said so, therefore my analogy is invalid. Did I get that right?