In this Golang article, we will write programs to demonstrate passing of pointers to a function suing increment and Swapping approach. A pointer stores the address of another variable. For ex- var *a = &b. Here a is the pointer that stores the address of b which means a can access the value of b as it stores the address of b. Using the Increment Approach In this method, we will write a Golang program to demonstrate the passing of pointers to a function by using an increment function where we use pointers to point to the variable which is ... Read More
In this article, we will write Golang programs to implement returning pointer from a function. A pointer stores the address of another variable and the address is depicted using & operator. Algorithm Step 1 − Create a package main and declare fmt(format package) package in the program where main produces executable codes and fmt helps in formatting input and Output. Step 2 − Create a function create_pointer that returns a pointer to the int Step 3 − Set the value of x in the function and return the variable back to the function Step 4 − Create a main ... Read More
In golang we can use various internal time function to parse time. Time is parsed using parse and ParseInLocation method. Syntax time.Parse() This function belongs to time package. It is used to parse the string into time.Time value and it takes two inputs: the layout and the time string which is to be parsed. time.ParselnLocation() This function is a part of the time package. It is used to parse the string into time.Time value along with a specified location. It takes 3 arguments: the location, layout string and the time string. time.Format() This function is present in ... Read More
The Golang Select statement is similar to the switch statement, the switch statement selects the Output based on the cases but here the Output is selected based on which communication occurs fast in the channels. Syntax func make ([] type, size, capacity) The make function in go language is used to create an array/map it accepts the type of variable to be created, its size and capacity as arguments. time.Sleep() This function belongs to the time package. Here, the word sleep depicts the meaning of the function which says that it will block the execution of goroutine for ... Read More
In golang, Promoted methods are the methods created under those structs which are embedded inside another struct, now the struct in which it is embedded can access its methods and fields. In this article we are going to explain how to show promoted methods using various structs like Rectangle and Square, Vehicle and car. Algorithm Step 1 − Create a package main and declare fmt(format package) package in the program where main produces executable codes and fmt helps in formatting input and Output. Step 2 − Create a Rectangle struct with two fields width and height of type float ... Read More
In golang, promoted fields are the structs that are embedded inside another structs and their fields can also be accessed the structs in which they are embedded. In this article we are going to understand three different approaches to depict use of promoted filed with and without the help of pointers. Using Pointers In this illustration, we will make use of pointers to show the promoted fields. The field names will be set with the help of pointers and information will be printed using dot notation. Algorithm Step 1 − Create a package main and declare fmt(format package) package ... Read More
In golang, a rand package is used to generate random numbers/characters. Firstly, it seeds with the current time to get the different random Output each time then perform any operation. Syntax rand.Seed(value) Rand.Seed() function is used to generate random numbers. It takes a user input as argument which is the upper limit for generating random numbers. func Now() Time The Now() function is defined in time package. This function generates the current local time. To use this function, we have to first impot the time package in our program. func (t Time) UnixNano() int64 The UnixNano() function ... Read More
In this article, we will write Go language programs to show scope of global and local variables using pointer approach. Scope is referred to as the accessibility of the piece of code in the program in particular function. Scope can be defined in two ways: globally and locally. Global means a variable declared in the main cannot be overwritten by any modification and remains same everywhere whereas local access means when variable is modified inside the function, it can only be used inside the function and its scope remains in that function only. Using Same Global and Local Variables In ... Read More
In this Golang article, we will write programs to print struct variables. Here, there is no concept of classes, instead of that structs are used to represent a collection of related fields or properties. Using Name of the Field and Shorthand Declaration in the Main Function In this method, we will create a child struct and further create two fields name and age in it. The field values are set by creating the instance of the struct. In the second example, the field values of the Child struct are set in the instance using shorthand declaration and the field ... Read More
This article teaches you how to write a Python program to find the size of the largest subset of anagram words Every character of the rearranged string or number must also be a component of another string or number in order for the condition of an anagram to exist. To put it another way, a string is said to be an anagram of another if the second is just the first string rearranged. As an illustration, the words The program and rogPrma are anagrams, as are the words Code and doCe. Input-Output Scenarios Lets take an input and its output ... Read More