Concatenate Two Slices in Golang

Mukul Latiyan
Updated on 22-Feb-2022 05:43:33

3K+ Views

Whenever we talk about appending elements to a slice, we know that we need to use the append() function that takes a slice as the first argument and the values that we want to append as the next argument.The syntax looks something like this.sl = append(sl, 1)Instead of appending a single number to the slice "sl", we can append multiple values in the same command as well.Consider the snippet shown below.sl = append(sl, 1, 2, 3, 4)The above code will work fine in Go.When it comes to appending a slice to another slice, we need to use the variadic function ... Read More

Sorting in Golang with Sort Package

Mukul Latiyan
Updated on 22-Feb-2022 05:40:50

635 Views

The standard library of Golang provides a package that we can use if we want to sort arrays, slices, or even custom types. In this article, we will discover three main functions that we can use if we want to sort a slice in Golang. We will also see how we can create a custom sort function and custom comparator.Let's first check how we can sort a slice of integer, float64 and string values.ExampleConsider the code shown below.package main import (    "fmt"    "sort" ) func main() {    integerSlice := []int{3, 2, 14, 9, 11}    sort.Ints(integerSlice)   ... Read More

Use iota in Golang

Mukul Latiyan
Updated on 22-Feb-2022 05:32:09

5K+ Views

Iota in Go is used to represent constant increasing sequences. When repeated in a constant, its value gets incremented after each specification. In this article, we will explore the different ways in which we can use iota in Go.Let's first consider a very basic example, where we will declare multiple constants and use iota.Example 1Consider the code shown belowpackage main import (    "fmt" ) const (    first = iota    second = iota    third = iota ) func main() {    fmt.Println(first, second, third) }OutputIf we run the command go run main.go, then we will get the ... Read More

Parse Date Strings in Golang

Mukul Latiyan
Updated on 22-Feb-2022 05:28:17

14K+ Views

When it comes to parsing Date strings in Go, we can use the Parse function that is provided by the time package. In Go, we don't use codes like most other languages to represent the component parts of a date/time string. Instead, Go uses the mnemonic device - standard time as a reference.For example, the reference time can either look like this −Mon Jan 2 14:10:05 MST 2020 (MST is GMT-0700)Or, it can look like this as well.01/02 03:04:10PM '20 -0700SyntaxThe syntax of the Parse() function is shown below.func Parse(layout, value string) (Time, error)The Parse function takes a layout and ... Read More

Anonymous Goroutines in Golang

Mukul Latiyan
Updated on 22-Feb-2022 05:22:37

4K+ Views

In order to be able to understand the anonymous goroutines, we must be aware of the existence of anonymous functions and goroutines. We will first explore the anonymous functions that are the real reason behind the motivation of anonymous goroutines and then we will learn a little about what goroutines are, before finally checking a few examples of anonymous goroutines.Anonymous functionsIn Golang, anonymous functions are those functions that don't have any name. Simply put, anonymous functions don't use any variables as a name when they are declared.We know that we declare a function with a similar syntax as shown below.func ... Read More

Generate Multiplication Table in Java

AmitDiwan
Updated on 21-Feb-2022 12:43:21

1K+ Views

In this article, we will understand how to print a multiplication table. Multiplication table is created by iterating the required input 10 times using a for loop and multiplying the input value with numbers from 1 to 10 in each iteration.Below is a demonstration of the same −InputSuppose our input is −Input : 16OutputThe desired output would be −The multiplication table of 16 is : 16 * 1 = 16 16 * 2 = 32 16 * 3 = 48 16 * 4 = 64 16 * 5 = 80 16 * 6 = 96 16 * 7 = 112 ... Read More

Round a Number to N Decimal Places in Java

AmitDiwan
Updated on 21-Feb-2022 12:39:49

379 Views

In this article, we will understand how to round a number to n decimal places. Rounding of decimal values are done using the CEIL or FLOOR functions.Below is a demonstration of the same −InputSuppose our input is −Input : 3.1415OutputThe desired output would be −Output : 3.2 AlgorithmStep 1 - START Step 2 - Declare a float variable values namely my_input. Step 3 - Read the required values from the user/ define the values Step 4 – Use the CEIL function to round the number to the required decimal places. In this example we are rounding up to 2 decimal ... Read More

Iterate Over Enum in Java

AmitDiwan
Updated on 21-Feb-2022 12:29:45

312 Views

In this article, we will understand how to iterate over enum objects. Enum is a datatype that represents a small collection of objects.Below is a demonstration of the same −InputSuppose our input is −Enum objects are defined as : red, blue, green, yellow, orangeOutputThe desired output would be −Printing the Objects: red blue green yellow orangeAlgorithmStep 1 – START Step 2 - Declare the objects of Enum function namely red, blue, green, yellow, orange Step 3 – Using a for loop, iterate over the objects of the enum function and print each object. Step 4- StopExample 1enum Enum { ... Read More

Return Element-wise Title Cased Version of String or Unicode in NumPy

AmitDiwan
Updated on 21-Feb-2022 11:05:26

130 Views

To return element-wise title cased version of string or unicode, use the numpy.char.title() method in Python Numpy. Title case words start with uppercase characters, all remaining cased characters are lowercase.The function title() returns an output array of str or unicode, depending on input type. The numpy.char module provides a set of vectorized string operations for arrays of type numpy.str_ or numpy.bytes_.StepsAt first, import the required library −import numpy as npCreate a One-Dimensional array of strings −arr = np.array(['kATIE', 'jOHN', 'Kate', 'AmY', 'brADley']) Displaying our array −print("Array...", arr)Get the datatype −print("Array datatype...", arr.dtype) Get the dimensions of the Array −print("Array Dimensions...", ... Read More

Get Itemsize of Masked Array in NumPy

AmitDiwan
Updated on 21-Feb-2022 11:03:07

200 Views

To get the itemsize of the Masked Array, use the ma.MaskedArray.itemsize attribute in Numpy. A masked array is the combination of a standard numpy.ndarray and a mask. A mask is either nomask, indicating that no value of the associated array is invalid, or an array of booleans that determines for each element of the associated array whether the value is valid or not.StepsAt first, import the required library −import numpy as np import numpy.ma as maCreate a numpy array using the numpy.array() method −arr = np.array([[35, 85], [67, 33]]) print("Array...", arr) print("Array type...", arr.dtype) print("Array itemsize...", arr.itemsize)Get the dimensions of ... Read More

Advertisements