jsonparser icon indicating copy to clipboard operation
jsonparser copied to clipboard

how to add item in slice?

Open cwww3 opened this issue 4 years ago • 0 comments

// Is this a right way?
func main() {
	var data = []byte(`[1,2,3]`)
	cnt := 2
	data,_  = jsonparser.Set(data,[]byte("3,4"),fmt.Sprintf("[%v]", cnt))
	fmt.Println(string(data))
	// Output: [1,2,3,4]
	tmp := []int{}
	_ = json.Unmarshal(data,&tmp)
	fmt.Println(len(tmp))
	// Output: 4
}

cwww3 avatar Nov 15 '21 07:11 cwww3