Found 1082 Articles for Go Programming

Golang program to show promoted methods

Akhil Sharma
Updated on 04-Apr-2023 12:06:19

314 Views

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

Golang program to depict use of promoted fields

Akhil Sharma
Updated on 04-Apr-2023 12:05:17

304 Views

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

Golang program to show use of rand package

Akhil Sharma
Updated on 04-Apr-2023 12:02:21

228 Views

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

Golang program to show scope of variables

Akhil Sharma
Updated on 04-Apr-2023 12:01:20

214 Views

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

Golang program to print struct variables

Akhil Sharma
Updated on 04-Apr-2023 11:59:57

963 Views

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

Golang program to check a hash collection is empty or not

Akhil Sharma
Updated on 03-Apr-2023 13:54:45

167 Views

In this article, we will write a Go language programs to check a hash collection is empty or not. A hash collection is a data structure that contains key-value pairs in Go (Golang) and enables quick lookups, additions, and deletions. The value that corresponds to the key can be accessed by hashing the key into an index. Hash collections in the form of maps have built-in support in Go. Here, a map is a reference type that is declared by using the map keyword, followed by the key type and value type in the format. Syntax func make ... Read More

Golang program to remove all items from hash collection

Akhil Sharma
Updated on 03-Apr-2023 13:52:55

194 Views

In Golang we can use delete keyword or empy map to remove all the elements from hash collection. Hashmap is a data structure from hash collection. It is an implementation of hash table which takes O(1) constant time to execute. In this program we will remove all items from hash collection using two methods. 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. func range(variable) The range function is used to iterate over ... Read More

Golang program to get the size of the hash collection

Akhil Sharma
Updated on 03-Apr-2023 13:52:10

262 Views

In Golang we can use various internal functions or packages like len() function or reflect package, to get the size of the hash collection. A Hash collection contains a hashmap which is generally used to create key:value pairs and on the basis of that it shortens the execution time. In this program, we have used two examples to get the size of hash collection. 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. func ... Read More

Golang program to search an item in the hash collection

Akhil Sharma
Updated on 03-Apr-2023 13:31:07

171 Views

In this article, we will write Golang programs to search an item in the hash collection. A hashmap is part of hash collections. Here we are building a hashmap as set of key-alue pairs using Golang’s map which is constructed using map literal. We will execute this program using two examples. Syntax func range(variable) The range function is used to iterate over any data type. To use this we first have to write the range keyword followed by the data type to which we want to iterate and as a result the loop will iterate till the last element ... Read More

Golang program to iterate hash collection and print only values

Akhil Sharma
Updated on 03-Apr-2023 13:23:09

227 Views

In golang we can use hashmap and key functions to iterate hash collection. Hashmap is said to be one of the hash collections. In the hashmap the data is stored in key value pairs as we did below. In this program we will use three methods to iterate the hashmap and print its values. 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 func append(slice, element_1, element_2…, element_N) []T The append function is ... Read More

Advertisements