It's handy behavior, because it let you write
x , err := method1()
if err !=nil {
...
}
y , err := method2()
if err !=nil {
...
}
instead of having to have multiple error variables.
On Wednesday, January 14, 2026 at 9:38:39 AM UTC-8 Robert Engels wrote:
> Yes, I was using shadowing loosely.
>
> When you have
>
> var x int
> … 100 lines of code …
> x,y := somefunc()
>
> And y is a new variable - you have no idea at the call site which is a new
> variable and which is not.
>
> That is the “shadowing” I’m referring to - loosely in that it obscures
> what is actually happening at the call site. You need to know other context
> - breaking Go’s desire for explicitness.
>
> > On Jan 14, 2026, at 11:23 AM, Jan Mercl <[email protected]> wrote:
> >
> > On Wed, Jan 14, 2026 at 6:09 PM Robert Engels <[email protected]>
> wrote:
> >
> >> Yes it does. You can declare new variables while assigning to old in Go.
> >
> > No it does not AFAICT. You wrote:
> >
> > """"
> > ... mixing of new and old declarations with new variables possibly
> > being introduced or old ones shadowed.
> > """"
> >
> > Please show a playground program where this happens. Note: shadowing
> > can happen only across blocks. The rules for short variable
> > declarations apply only within one block.
> >
> > Caveat emptor: Go specs do not define the term "shadowing". So you may
> > be using a very different definition of shadowing than most other
> > people have in mind when talking about shadowing variables in Go.
>
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion visit
https://groups.google.com/d/msgid/golang-nuts/c8bb7987-500f-464e-9741-bc8054076d26n%40googlegroups.com.