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

Slice - add function to delete item #43185

Closed
PawelPru opened this issue Dec 14, 2020 · 6 comments
Closed

Slice - add function to delete item #43185

PawelPru opened this issue Dec 14, 2020 · 6 comments

Comments

@PawelPru
Copy link

What version of Go are you using (go version)?

$ go version
go version go1.15.6 windows/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\Administrator\AppData\Local\go-build
set GOENV=C:\Users\Administrator\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\Administrator\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\Administrator\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=c:\go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=c:\go\pkg\tool\windows_amd64
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\ADMINI~1\AppData\Local\Temp\2\go-build519370390=/tmp/go-build -gno-record-gcc-switches

What did you do?

Trying to remove first item

import "fmt"

func main() {

maps := map[int]string{1: "a", 2: "b", 3: "c"}
delete(maps, 1)
fmt.Println(maps)

slice := []string{"a", "b", "c"}
delete(slice, 1)
slice[1].remove
slice[1].delete
fmt.Println(slice)

}

What did you see instead?

error

@PawelPru
Copy link
Author

Operation on slices should be simple as for map

@PawelPru PawelPru changed the title add to slice function to delete item Slice - add function to delete item Dec 14, 2020
@davecheney
Copy link
Contributor

davecheney commented Dec 14, 2020

@PawelPru the thing is, removal from a map has O(1) complexity whereas removing from a slice has O(n) complexity. If we made removals from both maps and slices look the same, we’d obscure from programmers the very different costs those operations have.

@seankhliao
Copy link
Member

see also https://github.com/golang/go/wiki/SliceTricks

@PawelPru
Copy link
Author

@davecheney anyhow if you want to remove item from slice you need to make additional operations. Right now such function dosn't exist and instead of simplicity we have complexity. Trick with slices may help but should be build-in

@davecheney
Copy link
Contributor

@PawelPru i don’t disagree, I just wanted to raise this point as this is part of Go’s general implicit policy of not hiding the cost of operations behind magic. I encourage you to search the issue tracker for previous discussions in this area as there have been many.

@ianlancetaylor
Copy link
Contributor

I agree with @davecheney . I'll add that this is something to be solved with generics, not with a change to the language.

@toothrot toothrot closed this as completed Jan 5, 2021
@golang golang locked and limited conversation to collaborators Jan 5, 2022
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

6 participants