Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spec: negative value for slice index #11245

Closed
mattn opened this issue Jun 17, 2015 · 1 comment
Closed

spec: negative value for slice index #11245

mattn opened this issue Jun 17, 2015 · 1 comment

Comments

@mattn
Copy link
Member

mattn commented Jun 17, 2015

hope equivalent to python's arr[1:-1]. for example:

arr := GetItems()
fmt.Println(arr[1:len(arr)-1])

we need to take arr as variable. but i want

part_of_arr := GetItems()[1:-1]
@adg adg changed the title feature requests: negative value for slice index proposal: negative value for slice index Jun 17, 2015
@adg
Copy link
Contributor

adg commented Jun 17, 2015

The omission of this feature (present in Python, for example) is deliberate

When performing arithmetic on slice indices it would be unfortunate if an erroneous negative result "just worked" as a reverse index. This leads to subtle bugs.

There are readability benefits to the status quo, also. It is clear that the Go expression s[:i] is creating a slice of s that is i bytes long. If i could be negative then the reader would need more context to understand the slice expression.

This is in keeping with Go's general philosophy of avoiding subtle syntactic tricks.

@adg adg closed this as completed Jun 17, 2015
@mikioh mikioh changed the title proposal: negative value for slice index spec: negative value for slice index Jun 17, 2015
@golang golang locked and limited conversation to collaborators Jun 25, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants