Found 1082 Articles for Go Programming

Golang program to get keys from a hash collection

Akhil Sharma
Updated on 27-Mar-2023 09:57:59

378 Views

A hash collection is a data structure that contains key-value pairs in Go 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. In tis example, we will use two methods to get keys from a hash collection, in the first example we will use append method to ... Read More

Golang program to get key based on value from the hash collection

Akhil Sharma
Updated on 27-Mar-2023 13:05:27

355 Views

In golang, we can use hashmap to perform various operations like deleting, updating, and storing the values in the hash collection. In this article, we are going to understand get key based on a value from the hash collection using two different examples. In the first example, we will use an external function and iterate the map to get the key from the value, in the second example we will use a reverse map to get the key from the desired value. Algorithm Create a package main and declare fmt(format package) in the program where main produces executable codes ... Read More

Golang program to add items into the hash collection

Akhil Sharma
Updated on 27-Mar-2023 09:52:12

142 Views

In Golang, we can use a simple indexing method and slicing method to add items to the hash collections. A hash function is used for retrieving and storing the data. In this article are going to see two different examples where we are going to use the above-mentioned methods to add items to a hash collection in go programming language. 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. Algorithm Import the required ... Read More

Golang program to include a module inside the class

Akhil Sharma
Updated on 01-Mar-2023 12:00:19

99 Views

In Go programming language, a new module is initiated using init command after which we can import other modules in our code. There is no concept of class in Go, only structs are used. In this article, we will use one example to include a module inside the class. In this example, we will create a Circle struct whose area will be printed on the console. For this example, we will import math and fmt packages outside the struct but they can be used anywhere. The print statement is executed using Printf function from the fmt package. Algorithm Import ... Read More

Golang program to create a block using curly braces

Akhil Sharma
Updated on 01-Mar-2023 11:59:43

519 Views

We can create a block in golang using curly braces and then create a variable inside the block to have the scope only inside the block and not outside it. In this article, we will use three examples to create a block using curly braces. In the first example few statements will be printed inside and outside the block, in the second example comparison value will be printed inside the block and in the third example function will be used to demonstrate the use of block. Algorithm Import the required packages in the program Create a main function In ... Read More

Golang program to convert the hash collection into string

Akhil Sharma
Updated on 01-Mar-2023 11:58:41

650 Views

Golang has json package that is used for converting the hask collection into strin. A hashmap belongs to hash collection which stores the data as key:value pairs, whereas a string is a sequence of characters and it is immutable. In this article, we will use two examples to convert the hash collection into string. In the first example, the Json package will be used to convert the map to encoded byte array whereas in the second example sort package will be use along the loops. Syntax json.Marshal() This function belongs to the JSON package and it converts the value ... Read More

Golang program to create a module with constant

Akhil Sharma
Updated on 19-Jul-2023 14:27:09

48 Views

In the Go programming language, a module is a collection of packages and it helps to manage them and their dependencies. In this article, we will create a module with constant using two examples. In the first example, we will create a constant string that will be returned by the function and that function will be called from the main of another module. In the second example, a function will be created to return the string and print when called by the main of another module. Algorithm Import the required packages in the program Create a constant string in ... Read More

Golang program to demonstrate the time arithmetic

Akhil Sharma
Updated on 01-Mar-2023 11:56:22

444 Views

We are going to calculate the current time using the Now function from time package in Go programming language for demonstrating the time arithmetic. The time arithmetic is calculated using mathematical functions. In both examples, we will use mathematical calculations to find the past, future and the duration using Sub function. The output is printed using Println function from the fmt package. Syntax 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 import the time package in our program. func sub() ... Read More

Golang program to get current working directory

Akhil Sharma
Updated on 22-Feb-2023 15:32:35

5K+ Views

Golang has a large range of internal packages to work with the directory. The directory in which the application is operating is known as the current working directory. Here we can use the OS packages as well as the path/filepath package of Golang to get current working directory. In the OS package we can use os.Getwd() function to the current working director. Whereas in path/filepath package we can use filepath.Abs and filepath.dir to get the filepath of the current working directory. Method 1: Using os.Getwd() function This method uses the os.Getwd function from the os package to get ... Read More

Golang program to create string from contents of a file

Akhil Sharma
Updated on 22-Feb-2023 15:31:47

121 Views

In go we can use io and os package to perform various operations with file. In this article, we are going to use ioutil.readfile function to read the file and then the string function to convert the file data to string. From OS package we are going to use os.open to open the file and use string operation to convert the data to string. Method 1: Using io/ioutil package In this illustration, the file's contents are read into a byte slice using the ioutil.ReadFile function. The byte slice is subsequently transformed into a string using the string function. The contents ... Read More

Advertisements