Hacker News new | past | comments | ask | show | jobs | submit login

WTF?! Isn't the point of an abstraction to make simple what is complex? Insert into a list should always be like the python example. If go's standard list doesn't have that, maybe it's time for someone to write a better library.



That's one of the things in my original comment.

Although I agree with you about some basic things in Go such as insertion being somewhat crazy, but there's a point I want to make about abstractions.

Abstractions that hide too many things away are not always a good thing.

If an abstraction makes something easier to write, but harder to read, that's not a very good thing.

Unfortunately Go does not exactly hit the sweet spot. It errs on the side of less expressive power.

EDIT:

Actually, for inserting into a list, the code can be simple, although the line will be split into three lines, and creates a new temporary list

// insert number 10 into position 2 var b []int b = append(b, a[0:2]...) b = append(b, 10) b = append(b, a[2:]...)

https://play.golang.com/p/ommopBd3io

Now, that _is_ overly verbose, but I don't think it's too off putting. I don't like it but I guess it's a quirk of go I'm willing to put up with.


The insertion operation on slices is expensive and is not recommended to be used frequently. If you use it frequently, please rethink and redesign your data structure, for example, use a list instead.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: