gop icon indicating copy to clipboard operation
gop copied to clipboard

Use `a <- v` instead of `a = append(a, v)`

Open xushiwei opened this issue 10 months ago • 3 comments

Proposal

In Go:

a = append(a, v)
a = append(a, v1, v2, v3)
a = append(a, b...)

In Go+:

a <- v
a <- v1, v2, v3
a <- b...

Easier to understand.

xushiwei avatar Feb 15 '25 10:02 xushiwei

but in a 'for loop' statement, the syntax 'a <- v' means 'a' will iterate through each element of v, which can be a little confusing if we reuse '<-' to replace a slice 'append()'.

proposal: it might be better to use 'for a in v' instead of 'for a <- v'.

akofer avatar Mar 09 '25 06:03 akofer

but in a 'for loop' statement, the syntax 'a <- v' means 'a' will iterate through each element of v, which can be a little confusing if we reuse '<-' to replace a slice 'append()'.

proposal: it might be better to use 'for a in v' instead of 'for a <- v'.

Related issue: https://github.com/goplus/gop/issues/295 (reopened)

xushiwei avatar Mar 09 '25 09:03 xushiwei

but in a 'for loop' statement, the syntax 'a <- v' means 'a' will iterate through each element of v, which can be a little confusing if we reuse '<-' to replace a slice 'append()'.

proposal: it might be better to use 'for a in v' instead of 'for a <- v'.

supported in https://github.com/goplus/gop/pull/2166

xushiwei avatar Mar 10 '25 00:03 xushiwei

@niupilot Please add related documentation to the quick start.

xushiwei avatar Oct 11 '25 02:10 xushiwei

Task Completed ✅

I've successfully added documentation for the <- append operator to the quick start guide.

What was done:

  • Added a new subsection "Appending to slices" in the Slices section of doc/docs.md
  • Documented all three forms of the <- append operator with examples:
    • a <- v - append single element
    • a <- v1, v2, v3 - append multiple elements
    • a <- b... - append another slice
  • Included comparison with Go's append() function for clarity

Pull Request

Created PR #2459: https://github.com/goplus/xgo/pull/2459


Branch: codeagent/claude/issue-2107-1760151315
Job Run: https://github.com/goplus/xgo/actions/runs/1

niupilot[bot] avatar Oct 11 '25 02:10 niupilot[bot]