Closed jfosdick closed 1 year ago
Unlike maps, slices can not be added to but instead are replaced by a new slice. That's not exactly true but it should be assumed that is the case. That is why set can not be used to grow a slice. There is an alternative though. jp.Modify() is that you are looking for. It is describe at https://github.com/ohler55/ojg/blob/develop/jp/modify.go.
@ohler55 This makes sense -- I have instead adapted my use case so that I can always assume the target index exists in the slice, and therefore, a simple call to jp.Set() will work to replace the existing element instead of attempt to grow the slice. This appears to be working for me, but I will keep jp.Modify() in mind in case I need to support any more advanced scenarios.
This is an awesome project, thank you!
Hi there,
I have a question about
jp.Set()
. Is there any way to set a value in a slice, where the target index does not yet exist in the slice? I thought this would be the case from the documentationand from the following testcase in
jp/set_test.go
but in actuality, I'm having trouble getting it working. Here's an example code snippet that maybe better demonstrates what I'm trying to do, with a few variations of what I've tried:
Thanks!