FAQ
One problem I just noticed with [:8] is that it might read beyond the
original length of the b.

var bytes [8]byte
b := b[:4]
binary.Uint64(b)

Used to panic, now it succeeds and reads (or writes, for PutUint64) all 8
bytes.
This is an argument for using _=b[7] instead. Not a terribly strong one,
but still.
On Thu, Mar 17, 2016 at 8:07 PM, Russ Cox wrote:

For what it's worth, b = b[:8] seems the clearest of the four you
suggested. The _= versions might also be made to work, but it seems more
confusing. And the ones setting the cap seem more complex for no reason. I
don't see why the compiler would need to know that cap is constant, and
almost no one uses that form, so it would look extra strange.

I checked that both the non-ssa and the ssa back ends know that b = b[:8]
only updates one of the three slice fields.

Russ

On Thu, Mar 17, 2016 at 7:14 PM 'Keith Randall' via golang-dev <
golang-dev@googlegroups.com> wrote:
See issue https://github.com/golang/go/issues/14849
I think we still don't know long-term the idiom we should use to tell the
compiler to do the bounds check first. We currently use b = b[:8]. But
there are lots of other options:
_ = b[:8]
b = b[:8:8]
_ = b[:8:8]

etc. I think we need to:
A) pick one, and
B) make sure the compiler does the right thing on the one we pick.

I was hesitant on 20654 because it does A (and de-facto announces it by
checking it into the std lib) without really doing B. I'd like to get B
done quickly so if we have to change our A decision for whatever reason, we
don't have to retrain anyone who saw A.


On Thu, Mar 17, 2016 at 3:15 PM, 'Austin Clements' via golang-dev <
golang-dev@googlegroups.com> wrote:
We do care about partial writes even in this case.
https://golang.org/ref/spec#Assignments gives exactly this example:

x[1], x[3] = 4, 5 // set x[1] = 4, then panic setting x[3] = 5.

(I'm slightly fuzzy on what in the text implies this, but the
Assignments section does say that assignments are carried out in
left-to-right order.)
On Thu, Mar 17, 2016 at 5:04 PM, minux wrote:

A related question, what if the code is written as:

b[0], b[1], b[2], ... = byte(v), byte(v>>8), byte(v>>16), ....

could the compiler only do one bound check in this case?
do we need to care about partial writes in this case?

--
You received this message because you are subscribed to the Google
Groups "golang-dev" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to golang-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google
Groups "golang-dev" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to golang-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups
"golang-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to golang-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "golang-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 35 of 38 | next ›
Discussion Overview
groupgolang-dev @
categoriesgo
postedMar 10, '16 at 4:36p
activeMar 21, '16 at 1:09p
posts38
users11
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase