Append to a slice specification

228 views
Skip to first unread message

Brendan Tracey

unread,
Apr 1, 2013, 4:51:27 PM4/1/13
to golan...@googlegroups.com
The spec (at tip and 1.0.3) states:

"If the capacity of s is not large enough to fit the additional values, append allocates a new, sufficiently large slice that fits both the existing slice elements and the additional values. Thus, the returned slice may refer to a different underlying array."

Should this be an "If and only if"? For example, if I know the capacity of my slice is sufficiently long, am I assured that I will not change the underlying array?

Rob Pike

unread,
Apr 1, 2013, 4:52:44 PM4/1/13
to Brendan Tracey, golan...@googlegroups.com
Yes you are so assured.

Brendan Tracey

unread,
Apr 1, 2013, 4:54:09 PM4/1/13
to Rob Pike, golan...@googlegroups.com
Thanks. Should the spec be modified to clarify that?

Daniel Jo

unread,
Apr 1, 2013, 5:10:09 PM4/1/13
to Brendan Tracey, Rob Pike, golang-nuts

I think that "and only if" is redundant in the context of a programming language.

-Daniel

--
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 golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Dave Cheney

unread,
Apr 1, 2013, 5:12:27 PM4/1/13
to Brendan Tracey, Rob Pike, golang-nuts
Can you explain why you need the additional clarification ? Are you
trying to not store the value returned from append ?

Dan Kortschak

unread,
Apr 1, 2013, 6:20:27 PM4/1/13
to Dave Cheney, Brendan Tracey, golang-nuts
I can see cases where you might want to append into a slice up to its
cap where the slice is provided by the caller. Here you would want to
know that the caller's slice remains pointing to the array you were
appending into. (copy may be better here though).

Brendan Tracey

unread,
Apr 1, 2013, 6:22:45 PM4/1/13
to Dan Kortschak, Dave Cheney, golang-nuts
Possibly, but if you're trying to minimize the amount of garbage created...

Dan Kortschak

unread,
Apr 1, 2013, 6:25:10 PM4/1/13
to Brendan Tracey, Dave Cheney, golang-nuts
No, absolutely. Though you can still copy to mimic that. I agree that
this should be clarified. File an issue?

Dan Kortschak

unread,
Apr 1, 2013, 6:26:05 PM4/1/13
to Daniel Jo, golang-nuts
Why? iff and if are not the same.

Brendan Tracey

unread,
Apr 1, 2013, 6:38:34 PM4/1/13
to golan...@googlegroups.com, Brendan Tracey, Dave Cheney
Can you show what you mean by "you can still copy to mimic that"? I don't follow

Issue  5180 filed:

Dan Kortschak

unread,
Apr 1, 2013, 6:48:18 PM4/1/13
to Brendan Tracey, golan...@googlegroups.com, Dave Cheney
http://play.golang.org/p/8ZkPPj1Fd0

Add in any bounds checks you might want. Also, obviously if you are
adding single elements rather than slices, you need to wrap them in a
slice (may make garbage - but the compiler should be smart enough).

I think this is pretty similar to how container/vector used to implement
append (with the obvious addition of growing).

Brendan Tracey

unread,
Apr 1, 2013, 6:56:13 PM4/1/13
to golan...@googlegroups.com, Brendan Tracey, Dave Cheney
Ah, that's neat!

I've gotten the hang of coding go, but I'm still figuring out how to code go well, especially as it relates to minimizing garbage and designing APIs which are cognizant of that fact.

Daniel Jo

unread,
Apr 1, 2013, 7:54:55 PM4/1/13
to Dan Kortschak, golang-nuts
Ah, I stand corrected. I just looked it up. That was probably in that logic class that I never finished, many years ago.

-Daniel
Reply all
Reply to author
Forward
0 new messages