Split a Slice into Two Halves in Go

Akhil Sharma
Updated on 13-Feb-2023 15:58:37

3K+ Views

A slice can also be called as a dynamic array as it's value is dynamic whereas normal array is static. This makes slice more efficient and faster. They are passed by reference instead by value. Let us learn how to split a slice into two halves using Golang program. Syntax func append(slice, element_1, element_2…, element_N) []T The append function is used to add values to an array slice. It takes number of arguments. The first argument is the array to which we wish to add the values followed by the values to add. The function then returns the final ... Read More

Golang Program to Merge Two Slices

Akhil Sharma
Updated on 13-Feb-2023 15:57:29

6K+ Views

In Golang, a slice is a sequence of elements just like an array. An array is a fixed sequence of elements whereas slice is a dynamic array which means its value is not fixed and can be changed. Slices are more efficient and faster than arrays moreover they are passed by reference instead by value. Syntax func append(slice, element_1, element_2…, element_N) []T The append function is used to add values to an array slice. It takes number of arguments. The first argument is the array to which we wish to add the values followed by the values to add. ... Read More

Replace Character at Specific Index in Golang

Akhil Sharma
Updated on 13-Feb-2023 15:46:44

2K+ Views

In this tutorial, we will inculcate how to replace a character at a specific index using few examples. The output will be printed on the console using fmt.Println() function. Let’s dive deep into the examples and see how it’s done. Method 1: Using Replace Function In this method, we will learn how to replace a character at a specific index using replace function. The output is printed on console using fmt.Println() function. Let’s understand this through the code. Syntax func Replace(str, oldstr, newstr string, m int) string This function is used to return the copy of the string which ... Read More

Remove All Nil Elements from Array in Golang

Akhil Sharma
Updated on 13-Feb-2023 15:44:10

2K+ Views

This tutorial is all about how to remove all nil elements from the array. Basically, sometimes there are empty strings present in the array which we want to remove. Here in this tutorial, we will have a look how to remove those strings with the use of very simple methods. Method 1: Using helper Function Approach In this method, we will see how to remove null elements using helper function approach. The array will be taken as a parameter in the function and the output will be printed on console using print statement of Golang. Syntax func append(slice, element_1, element_2…, ... Read More

Golang Program to Iterate Over a Slice

Akhil Sharma
Updated on 13-Feb-2023 15:38:02

3K+ Views

In this tutorial, we will iterate over a slice using different set of examples. A slice is a dynamic sequence which stores element of similar type. The iterated list will be printed on the console using fmt.Println() function. Method 1:Using for Loop with Index In this method, we will iterate over a slice using for loop where both the index and its element will be printed on the console screen. Let’s have a look how to execute this example. Algorithm Step 1 − Create a package main and import fmt package in the program. Step 1 − Create a package ... Read More

Fill an Array with a Specific Element in Golang

Akhil Sharma
Updated on 13-Feb-2023 15:35:44

2K+ Views

In this tutorial, we will learn how to fill an array with a specific element. We will explore few examples to know more about this program. The output will be printed on console using fmt.Println() function. Let’s have a look and understand the program. We have used the following make() function in the examples. 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. Method 1: Using for Loop and Make Function in Main Function ... Read More

Find Prime Numbers from an Array in Golang

Akhil Sharma
Updated on 13-Feb-2023 15:31:52

499 Views

In this tutorial, we will showcase how to find the prime numbers from the array using different examples. Prime numbers are those numbers which are either divisible by 1 or by themselves they have no factor other than this. Now we will present you few illustrations to help you get clear with the logic behind this program. Method 1: Finding Prime Number in the main Function In this particular method, we will print the prime numbers with the help of nested for loops. We will half the array elements in every inner iteration and check whether they are divisible or ... Read More

Fetch Elements from an Array Based on Index in Golang

Akhil Sharma
Updated on 13-Feb-2023 15:26:59

890 Views

In this tutorial, we will learn how to fetch elements from an array with the help of index using different examples to showcase what methods can be applied to it and the result obtained will be printed on the console using the print function in Golang. Method 1: Using an Array Index In this method, we will see how to fetch element from an array using index in for loop and this is one of the simplest methods to solve this problem, lets have a look at this example to understand it. Algorithm Step 1 − Create a package main ... Read More

Remove Given Element from Array in Golang

Akhil Sharma
Updated on 13-Feb-2023 15:25:24

4K+ Views

In this tutorial, we will learn how to remove an element from an array using simple for loop approach. The logic behind this approach is that create a new array and the particular index element which is to be removed don’t add it to the new array. Let’s have a look how to execute it with the use of different examples. Remove Element from the Array in main Function In this method, we will execute the entire program inside main function. An original array and a new array will be created to execute the removal of elements from the array. ... Read More

Golang Program to Illustrate the Creation of Strings

Akhil Sharma
Updated on 13-Feb-2023 15:21:26

172 Views

In this tutorial, we will see how strings are created in Golang. There are different ways to create strings which we will learn via some examples. In Golang a string is a sequence of variable-width characters where each character is represented by one or more bytes. Method 1: Using double Quotes with Shorthand Declaration In this method, we will learn about how to create strings using shorthand declaration. Let’s dive into the code to understand it. Algorithm Step 1 − Create a package main and import fmt package in the program. Step 2 − Create a function main and further ... Read More

Advertisements