From: Ralph Corderoy <ralph@inputplus.co.uk>
To: coff@tuhs.org
Subject: [COFF] Shadowing variables in Go.
Date: Tue, 01 Oct 2024 14:20:59 +0100 [thread overview]
Message-ID: <20241001132059.511CD1FB21@orac.inputplus.co.uk> (raw)
In-Reply-To: <202410011243.491ChKiV419651@freefriends.org>
Taken to COFF...
Hi Arnold,
> In main(), I *think* I'm assigning to the global clientSet so that
> I can use it later. But because of the 'err' and the :=, I've
> actually created a local variable that shadows the global one, and in
> otherfunc(), the global clientSet is still nil. Kaboom!
>
> The correct way to write the code is:
>
> var err error
> clientSet, err = cluster.MakeClient() // or whatever
I think this is a common problem when learning Go, like assigning
getchar()'s value to a char in C. It was back in ’14 anyway, when I saw
https://www.qureet.com/blog/golang-beartrap/ which has an ‘err’ at an
outer scope be unwritten by the ‘:=’ with the new, assigned-to ‘err’
going un-checked.
The author mentions ‘go vet’ highlights these cases with -shadow, which
is off by default.
https://pkg.go.dev/github.com/golangci/govet#hdr-Shadowed_variables
suggests that's still the case.
--
Cheers, Ralph.
next parent reply other threads:[~2024-10-01 13:21 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <CAKH6PiUS7RjkCyuYREFk4Li+aORaEZ_Vu7Dg1nnsFuHJF+mqog@mail.gmail.com>
[not found] ` <CAKzdPgwJ7-_BWztOQKiB6h5a+OGwXtefsD47Fq+GDwGGF7N4UA@mail.gmail.com>
[not found] ` <CAFH29tqBw0Qpzqv=21SUZonL7w_tpB0i4Ts8PwQ-Akn5PvTvVw@mail.gmail.com>
[not found] ` <CAKzdPgzQTjruZ3Cc-Sfram5M=jM+emvsTGR+7GRtZUUzNhZOCg@mail.gmail.com>
[not found] ` <20240930003630.GE17434@mcvoy.com>
[not found] ` <e23f7d87-1d17-f484-74ad-53deb5cc278f@makerlisp.com>
[not found] ` <d2d70aa8-0d7c-0a1e-9152-a4f8c50ae33d@makerlisp.com>
[not found] ` <202410011243.491ChKiV419651@freefriends.org>
2024-10-01 13:20 ` Ralph Corderoy [this message]
[not found] ` <20240930191216.tIpea9lo@steffen%sdaoden.eu>
[not found] ` <CACY3YMHzg+6U_zTuhMTORgfh_Kse6MTspaGDfuUjXb4vLvV9mw@mail.gmail.com>
[not found] ` <CAEoi9W46LRBBxQ22kM8kmvqHeTetXkp=jfUeDC+qu0kbvv+swA@mail.gmail.com>
[not found] ` <vhfa99$114r$1@ciao.gmane.io>
2024-11-22 1:53 ` [COFF] Re: [TUHS] Re: Minimum Array Sizes in 16 bit C (was Maximum) Dan Cross
[not found] ` <CAK7dMtC=y+a6=vk_zrQSA659h8=td0YBpd0BTvxmAJowefm_VA@mail.gmail.com>
2024-11-26 19:51 ` Dan Cross
2024-11-26 20:15 ` Larry McVoy
2024-11-27 1:03 ` Steffen Nurpmeso
2024-11-26 21:38 ` John Levine
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20241001132059.511CD1FB21@orac.inputplus.co.uk \
--to=ralph@inputplus.co.uk \
--cc=coff@tuhs.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).