When to choose container/list over slice?

I have not faced any problem, which can’t be solved by slice.

As per the list implementation, Any insertion to list end-up creating a new element struct which wraps the value to interface{}
Range is also not supported for iteration of list.
I found some articles saying “never use list.List at production.”

Just wondering, why is it there at go source package
https://golang.org/pkg/container/list/
Anybody here, used list at production and gained some advantages?

I assume people will benefit from List in Go exactly as in any other language. Slice are based on arrays with capacity, list are free to insert anywhere. It could prove quicker when array have to be moved in memory to extend the capacity and in some delete situations. You can read this article about the comparison of both : https://dzone.com/articles/performance-of-array-vs-linked-list-on-modern-comp

1 Like

Including container/list in the standard library was probably a mistake at the time. If it seems like the right thing to use I would first try with a slice and see if that isn’t good enough. If it’s not, you probably want something custom and type safe - not the container/list.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.