Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

[Go src] why forcing nil to err variable and not using

16 views
Skip to first unread message

Timothy Quinn

unread,
Dec 27, 2017, 12:36:06 AM12/27/17
to Camlistore
I am completely a N00b for Go so please pardon my ignorance. I'm reverse engineering some code and came across the following line in camput/blobs.go which had me scratching my head: 

bref, err = blob.RefFromHash(s1), nil

Why is this written this way? Is this just a one line trick to clear out error instead of using two lines of code like:
err = nil
bref = blob.RefFromHash(s1)

Mathieu Lonjaret

unread,
Dec 27, 2017, 6:19:32 AM12/27/17
to camli...@googlegroups.com
you did not quote more of the context, which was important in this
case. The relevant bits are:

sz, err := io.CopyN(s1, contents, constants.MaxBlobSize+1)
if err == nil || err == io.EOF {
bref, err = blob.RefFromHash(s1), nil
} else {

that, and the fact that err is a named returned parameter in this function.
I believe the intent here is to treat an io.EOF during the io.Copy as
a non error. So in the case of an io.EOF you want to set the function
level err to nil, so that any subsequent (naked) return will return a
nil error.

And yes, as you guessed, the effect is the same as if it had been
written as two separate lines (which is probably what i would have
done).
I believe you can set as many vars you want on the same line that way, such as:

a, b, c = foo, bar, baz
> --
> You received this message because you are subscribed to the Google Groups
> "Camlistore" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to camlistore+...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Brad Fitzpatrick

unread,
Dec 27, 2017, 11:36:00 AM12/27/17
to camli...@googlegroups.com
On Tue, Dec 26, 2017 at 9:36 PM, Timothy Quinn <tim.c...@gmail.com> wrote:
I am completely a N00b for Go so please pardon my ignorance. I'm reverse engineering some code

Reverse engineering or reading? :)

If you have questions about how something works, please ask. The code is there also, of course, but don't go to extreme efforts to find something if we can point you in the right direction instead.
 
and came across the following line in camput/blobs.go which had me scratching my head: 

bref, err = blob.RefFromHash(s1), nil

Why is this written this way? Is this just a one line trick to clear out error instead of using two lines of code like:
err = nil
bref = blob.RefFromHash(s1)

Timothy Quinn

unread,
Dec 27, 2017, 11:26:49 PM12/27/17
to Camlistore
> Reverse engineering or reading? :)

The more I read the docs, its definitely feels more like reading. Great job!

Thanks for the cleanup on that code. Will be great for GoN00bs like me ;)

Reply all
Reply to author
Forward
0 new messages