Wednesday, March 29, 2023

How To Read The Values From Empty Interface Used During The JSON.Unmarshal in Go Language

When we don't know the type of the json structure or it's in dynamic nature usually we will use empty interfaces to handle these types.

Whenever you want to read values using the key from this empty interface ideally we need to convert this interface to map type interface using the type conversion.

Once it's converted to map we can use the map["keyname"] format to get the corresponding value from the json. 


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..!

How To Convert An Interface To String in Go Lang

 If you're trying to convert an interface{} or map of interface values to string use the fmt.Sprint() function. 

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..!

Friday, March 24, 2023

How To Convert Array to Slice in Go Language

Array is a fixed in size and can't be increased/decreased the size at run time where as Slices are dynamic in nature like can be decreased or increased the size automatically at runtime basis on the number of elements added to it.

To convert the array to slice at anytime we can use simply :(dot notation) shown in below


Source Code:

package main

import (
	"fmt"
	"reflect"
)

func main() {
	arr := [4]string{"go", "lang", "main", "sree"}
	fmt.Println("Arr is of type...", reflect.TypeOf(arr))
	arr2 := arr[:]   //Converts arr of type array to slice arr2
	fmt.Println("Arr2 is of type..", reflect.TypeOf(arr2))
}

Output:

Arr is of type... [4]string
Arr2 is of type.. []string




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..!




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..!