Thursday, March 9, 2023

How To Add List/Set of Records to List in Go Language

In Go language if we want to add single record/list of records we can use the append() method. But here is the trick adding the single item is pretty much forward and while adding the list/set of records we might face some issue.

Now we will discuss how to handle both the scenarios

Adding a single item/record to list/array:

package main

func main() {
	
	var finalItemList []string
	var singleItem string
	finalItemList = append(finalItemList, singleItem)

}

Adding multiple items/list to another list/array:

To add list of items to another list with help of append() we will receive an error as can not use variable of type of list/array as a data type value in argument to append.

To solve this simply we can just use Spread operator ... (3 dots) notation as shown below.

package main

func main() {
	
	var finalItemList []string
	var secondItemList []string
	finalItemList = append(finalItemList, secondItemList...)

}


Please comment or write us if you have any queries/requirements.

Please like,follow,bookmark,subscribe this site to receive daily updates.


FaceBook Page - I Love Coding. You?


Hope this helps you..Enjoy..!




No comments:

Post a Comment