1

I tried a few options but with no results. If anyone knows some kind of solution it would be nice. I was trying with buger/jsonparser because of the parsing speed. Lets say i want to exclude object3 and assign it to MYVARIABLE

for exaple :

data:=[{object1}, {object2}, {object3}]

//this function iterates through the array
jsonparser.ArrayEach(data, func(key []byte, dataType jsonparser.ValueType, offset int, err error) {

    MYVARIABLE:=key

    return
})
1
  • Add some code as examples please. Commented Jan 27, 2017 at 11:37

1 Answer 1

1

Let's say that you have successfully parsed your data to struct. And you would have an array of yourStruct []yourStruct, and assign the third element with empty struct like this :

yourStruct[2] = YourStruct{}

the third element is still there with empty value. And unfortunately in go you can't assign struct with nil value.

or you can convert the []byte of your data that has your json to string and iterate it over to the their element and remove it with empty char, but this would be an expensive approach.

As Kaedys said you can remove your array struct using slice like this :

yourStruct = yourStruct[:2]
fmt.Printf("resutl struct = %+v\n", yourStruct)
Sign up to request clarification or add additional context in comments.

1 Comment

You can easily omit an entry from a slice though. yourStruct = append(yourStruct[:2], yourStruct[3:]...). As long as the slice has a length of 3 or more, that won't panic (even though index 3 in a slice of length 3 is an invalid index, the runtime is coded to handle it properly).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.