Found 1082 Articles for Go Programming

How to 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

467 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

How to 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

How to parse Date strings in Golang?

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

11K+ 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

3K+ 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

How to compare slices, structs and maps in Golang?

Mukul Latiyan
Updated on 21-Feb-2022 08:09:20

1K+ Views

The reflect package in Go provides a very important function called DeepEqual() which can be used to compare composite types. The DeepEqual() function is used when we want to check if two data types are "deeply equal".Comparing slicesExample 1Consider the code shown belowpackage main import (    "fmt"    "reflect" ) func main() {    sl := []int{1, 2, 3}    sl1 := []int{1, 2, 3}    fmt.Println(reflect.DeepEqual(sl, sl1)) }OutputIf we run the command go run main.go on the above code, then we will get the following output in the terminal.trueComparing mapsExample 2Consider the code shown below.package main import ( ... Read More

How to check if a string contains a substring in Golang?

Mukul Latiyan
Updated on 21-Feb-2022 08:16:10

668 Views

We know that substrings are a contiguous sequence of characters in a string, and in order to check whether a string contains a substring, we have two options available.The first approach is to use a built-in function called Contains() and the second approach is to make use a self-written logic for the same.The syntax for the Contains() function of the strings package is shown below.func Contains(s, substr string) boolIn the above syntax, there are two parameters inside the function Contains(). The first parameter is the string in which we are trying to find the pattern, and the second is the ... Read More

How to check if a key exists in a map in Golang?

Mukul Latiyan
Updated on 21-Feb-2022 07:32:37

710 Views

We know that maps in Go contain key-value pairs. There are often instances where we would want to know that a certain key exists in a map or not, in such cases, we have two options available.The first approach is very naive, as in this approach, we basically iterate over the map with the help of the range clause and then compare each key to the key that we want to check if is available or not.And the second approach is a bit better, as we make use of the if statement with a bit of syntactic sugar.Let's first check ... Read More

How to search and replace texts in a string in Golang?

Mukul Latiyan
Updated on 21-Feb-2022 07:28:20

956 Views

We often want to replace certain strings or all the strings that match a pattern with some other string. In order to do that in Golang, we can either use the native functions that the strings package of Go's standard library provides us with or we can write the logic for the same on our own.In this article, we will see different examples, where we will use the two most used functions of the strings package. These functions are −strings.Replace()strings.ReplaceAll()Let's first consider the signature of these functions to know a little more about them.Syntax of strings.Replace()func Replace(s, old, new string, ... Read More

How to read CSV files in Golang?

Mukul Latiyan
Updated on 21-Feb-2022 07:25:43

2K+ Views

To read a CSV file in Go, the first thing that we need to make use of is the encoding/csv package that the Go standard library provides us with. The encoding/csv package contains different functions and methods that can be used when we want to read data from a CSV file.In this article, we will use the NewReader() function that the package provides, which takes one argument and that is basically the file that we want to open and then invoke the ReadAll() method on the file as well.Besides the encoding/csv package, we will also use the os package, which ... Read More

Advertisements