Posts

Showing posts with the label Go Language

Exploring Go Language: Features, Advantages, and Limitations

Introduction: Go (also known as Golang) is an open-source programming language developed by Google. With its simplicity, concurrency support, and robustness, Go has gained significant popularity among developers. In this blog post, we will delve into the features, advantages, and limitations of Go language, shedding light on why it has become a preferred choice for building scalable and efficient applications. 1: Features of Go Language 1.1. Simplicity and Readability: Go follows a minimalistic approach, aiming for simplicity and ease of use. Its syntax is clean and concise, making it easy to read and understand. The language avoids unnecessary complexity, which helps reduce bugs and enhances code maintainability. 1.2. Concurrency and Goroutines: Go has built-in support for concurrency with Goroutines and channels. Goroutines are lightweight threads that allow developers to execute concurrent tasks efficiently. Channels facilitate communication and synchronization between Goroutines, s...

How to copy elements from one Map to another Map in Go Lang ?

Image
#JavaInspires How to copy elements from one Map to another Map in Go Lang ? Program to copy elements one map to another map in go language. Here, we are using for loop to copy each element to another map.  // golang program to copy elements one map to another map package main import "fmt" func main() { // create a map using shorthand declaration map1 := make( map [ string ] int ) // add elements to the map map1[ "john" ] = 22 map1[ "erik" ] = 25 map1[ "sean" ] = 24 map1[ "clark" ] = 26 // print map1 fmt.Println( "map1 :" , map1) // create empty map map2 := make( map [ string ] int ) // iterate map1 and copy each element to map2 for i, e := range map1 { map2[i] = e } // print map2 fmt.Println( "map2 :" , map2) } PS C:\DEVD\WORK\golang\example-programs> go run .\copymapexample.go map1 : map[clark:26 erik:25 john:22 sean:24] map2 : map[clark:26 erik:25 john:22 se...

Functions In Go Lang | Go Lang Examples

Image
#JavaInspires  ðŸ˜€ Hi Guys, Welcome to java inspires.  In this post, we will see how to create and work with functions in go lang. sample.go //JavaInspires package main import "fmt" func main() { // invoke simple function simpleFunction() // invoke function with parameters add( 2 , 3 ) //invoke function with return value ret := multiply( 2 , 3 ) fmt.Println( "multiply return value :" , ret) //invoke function with multiple return values add, sub := compute( 2 , 3 ) fmt.Println( "add :" , add) fmt.Println( "sub :" , sub) //invoke variadic function fmt.Println( "sum(1,2,3) = " , sum( 1 , 2 , 3 )) fmt.Println( "sum(1,2,3,4) = " , sum( 1 , 2 , 3 , 4 )) } // lets create a simple function func simpleFunction() { fmt.Println( "Simple Function Called" ) } // create a function with parameters func add(a, b int ) { fmt.Println( "a + b = " , a + b) } //function with return ...

Getting Input From Keyboard using Scanf | Go Lang Examples

Image
#JavaInspires  Hi Guys, Welcome to Java Inspires.. In this post, we will see how to read or get input from keyboard using Scanf function from fmt pacakge. sample.go package main import "fmt" func main() { //Provide some question fmt.Println( "What language you want to learn ?" ) // read answer using Scanf var answer string fmt.Scanf( "%s" , & answer) //print answer fmt.Println( "Great... you are learning" , answer, "language" ) } PS C:\Users\developer\Desktop> go run .\sample.go What language you want to learn ? Go... Great... you are learning Go... language PS C:\Users\developer\Desktop> THANK YOU #JavaInspires

Create A Slice Using Make Function And Add Items | Go Lang Examples

Image
#JavaInspires  Hi Guys, Welcome to Java Inspires. In this post, we will see how to create a Slice in go lang using make function and also how to add elements to the slice. sample.go package main import "fmt" func main() { //create a string type slice var strSlice = make ([] string , 5 ) // here length and capacity is 5 //print strSlice fmt.Println( "strSlice :" , strSlice) // print length of slice fmt.Println( "Length of strSlice :" , len (strSlice)) //print capacity of strslice fmt.Println( "Capacity od strSlice :" , cap (strSlice)) // lets create a slice with different length and different capacity var strSl = make ([] string , 5 , 8 ) // here 5 is length and 8 is capacity // print capacity and length fmt.Println( "Length of strS1 :" , len (strSl)) fmt.Println( "Capacity of strS1 :" , cap (strSl)) // now add element to the slice strSl[ 0 ] = "Hello" strSl[ 1 ] = ...

How To Create An Empty Slice In Go Lang ? | Go Lang Examples

Image
 Hi Guys, Welcome to Java Inspires.. In this post, we will see how to create an empty slice in go and how to add items to this empty slice.  sample.go package main import "fmt" func main() { // create empty slices var intSlice [] int // this is int slice var strSlice [] string // this is string slice // print these slices fmt.Println( "intSlice :" , intSlice) fmt.Println( "strSlice :" , strSlice) // print length of these slices fmt.Println( "Length of intSlice :" , len (intSlice)) fmt.Println( "Length of strSlice :" , len (strSlice)) // print capacity of these slices fmt.Println( "Capacity of intSlice :" , cap (intSlice)) fmt.Println( "Capacity of strSlice :" , cap (strSlice)) // add new elements to these slices intSlice = append (intSlice, 1 , 2 , 3 , 4 , 5 ) // can add any number of elements strSlice = append (strSlice, "Hello" , "World" , ...

How To Convert Integer To String In Go Lang | Go Language Examples | Java Inspires

 Hi Guys, Welcome to Java Inspires .. in this post we will see how to convert a variable from integer type to string type. goexample.go package main import ( "fmt" "reflect" "strconv" ) func main() { // lets start //To convert an integer to a string // create an int variable var int1 = 22 // print the type of int1 fmt.Println( "int1 type :" , reflect.TypeOf(int1)) fmt.Println( "Integer :" , int1) // convert into string - using Itoa fron strconv var str1 = strconv.Itoa(int1) fmt.Println( "str1 type :" , reflect.TypeOf(str1)) fmt.Println( "String :" , str1) } Output PS D:\MyCode\go\example> go run .\goexample.go int1 type : int Integer : 22 str1 type : string String : 22 PS D:\MyCode\go\example>

Find Type Of Variable In GoLang | Go Language Examples | Java Inspires

 Hi Guys, Welcome to Java Inspires .. In this post, we will see how to find the type of varible in go language. goexample.go package main import ( "fmt" "reflect" ) func main() { // lets start var var1 = 101 fmt.Println( "var1 type :" , reflect.TypeOf(var1)) var var2 = 12.12 fmt.Println( "var2 type :" , reflect.TypeOf(var2)) var var3 = true fmt.Println( "var3 type :" , reflect.TypeOf(var3)) var var4 = complex ( 2 , 3 ) fmt.Println( "var4 type :" , reflect.TypeOf(var4)) } Output: PS D:\MyCode\go\example> go run .\goexample.go var1 type : int var2 type : float64 var3 type : bool var4 type : complex128 PS D:\MyCode\go\example> '

Map Examle | Go Language Examples | Java Inspires

 Hi Guys, Welcome to Java Inspires .. Here is the example program of Map in go. goexample.go package main import ( "fmt" ) func main() { // lets start //create an empty map var m1 = make ( map [ string ] int ) //print the map fmt.Println( "Map :" , m1) //print length of the map fmt.Println( "Map length :" , len (m1)) // set eky value pairs into the map m1[ "key1" ] = 101 m1[ "key2" ] = 102 m1[ "key3" ] = 103 //print the map fmt.Println( "Map :" , m1) //print length of the map fmt.Println( "Map length :" , len (m1)) //get a value for a key v1 := m1[ "key3" ] //print the value fmt.Println( "Value " , v1) //get value for unavailable key v2 := m1[ "key5" ] fmt.Println( "v5 :" , v2) //remove a key/value pair from the map delete (m1, "key1" ) //print the map fmt.Println( "map m1:...

Go Language Examples | Hello World Example | Java Inspires

Image
😀 Hi Guys, Welcome to Java Inspires. In this post, we will see simple go language hello world example. Lets start.. Screenshot: helloworld.go package main import ( "fmt" ) func main() { fmt.Println( "Hello Java Inspires ..." ) } Output: PS D:\MyCode\go\helloworld> go run .\helloworld.go Hello World ... PS D:\MyCode\go\helloworld>