Found 1082 Articles for Go Programming

How to Replace characters in a Golang string?

Syed Abeed
Updated on 10-Mar-2022 07:14:14

5K+ Views

The strings package of Golang has a Replace() function which we can use to replace some characters in a string with a new value. It replaces only a specified "n" occurrences of the substring.In addition to Replace(), there is a ReplaceAll() function that replaces all the occurrences of a given substring with a new value.Syntaxfunc Replace(s, old, new string, n int) stringWhere, s is the given stringold is the string which we want to replacenew is the string which will replace the old stringn represents the number of characters which we want to replace in the given string.ExampleThe following example demonstrates ... Read More

How to use the Fields() function in Golang?

Syed Abeed
Updated on 10-Mar-2022 07:08:43

669 Views

The strings package of Golang provides a Fields() method, which can be used to split a string around one or more instances of consecutive whitespace characters.The Fields() function splits a given string into substrings by removing any space characters, including newlines. And it treats multiple consecutive spaces as a single space.Syntaxfunc Fields(s string) []stringWhere s is the string parameter.ExampleLet us consider the following example −package main import (    "fmt"    "strings" ) func main() {    // Initializing the Strings    string1 := " The Golang Programming Language "    // Display the Strings    fmt.Println("Input String:", string1) ... Read More

What is the EqualFold function in Golang?

Syed Abeed
Updated on 10-Mar-2022 07:00:55

494 Views

The EqualFold() function in Golang is an inbuilt function of strings package which is used to check whether the given strings (UTF-8 strings) are equal. The comparison is not case-sensitive. It accepts two string parameters and returns True if both the strings are equal under Unicode case-folding (i.e., case-insensitive), False otherwise.Syntaxfunc EqualFold(s, t string) boolWhere s and t are strings. It returns a Boolean value.ExampleThe following example demonstrates how to use EqualFold() in a Go program −package main import (    "fmt"    "strings" ) func main() {    // Intializing the Strings    R := "Welcome to Tutorialspoint" ... Read More

How to count the number of repeated characters in a Golang String?

Syed Abeed
Updated on 10-Mar-2022 06:57:24

4K+ Views

Count() is a built-in function is Golang that can be used to count the number of non-overlapping instances of a character/string in a string.Syntaxfunc Count(s, sep string) intWhere, s – Original Stringsep – Substring which we want to count.It returns an Integer value.ExampleThe following example demonstrates how you can use the Count() function in a Go program.package main import (    "fmt"    "strings" ) func main() {    // Initializing the Strings    x := "Golang Programming Language"    y := "Language"       // Display the Strings    fmt.Println("First String:", x)    fmt.Println("Second String:", y)   ... Read More

How to use ContainsAny() function in Golang?

Syed Abeed
Updated on 14-Mar-2022 05:41:25

718 Views

Golang has a built-in string function called ContainsAny() that we can use to check whether a specified string is present in a given string or not.ContainsAny() is completely different from Contains().Contains() is used to detect if a string contains a substring.ContainsAny() is used to detect if a string contains any characters in the provided string. Even if one character of the specified string is present in the original given string, then it returns True, else False.Syntaxfunc ContainsAny(s, chars string) boolWhere, s – Original Stringchars string – Substring where we define the string or characters.It returns a Boolean value.ExampleContainsAny() is case-sensitive, ... Read More

How to compare two strings in Golang?

Syed Abeed
Updated on 10-Mar-2022 08:53:01

4K+ Views

Golang has a built-in string function called Compare() that we can use to compare two strings. Here strings are compared using the lexicographical order.Syntaxfunc Compare(a, b string) intReturn TypesIf the strings (a == b), it returns 0.If the strings (a > b), then it returns 1If the strings (a < b), then it returns -1ExampleLet us consider the following example −package main // importing fmt and strings import (    "fmt"    "strings" ) func main() {    // Intializing the variables    var a1 = "a"    var a2 = "b"    var a3 = "welcome"    var a4 ... Read More

How to use Contains() function in Golang?

Syed Abeed
Updated on 10-Mar-2022 06:42:28

6K+ Views

Golang has a set of built-in string functions that we can utilize to perform different types of operations on string data. Contains() is such a function that can be used to search whether a particular text/string/character is present in a given string. If the text/string/character is present in the given string, then it returns True; else it returns False.Syntaxfunc Contains(s, substr string) boolWhere, s is the original string and substr is the string which is to be checked with the original string.ExampleThe following example demonstrates how Contains() works −package main // importing fmt and strings import (    "fmt"   ... Read More

How to check if a file exists in Golang?

Mukul Latiyan
Updated on 01-Nov-2023 02:12:56

36K+ Views

In order to check if a particular file exists inside a given directory in Golang, we can use the Stat() and the isNotExists() function that the os package of Go's standard library provides us with.The Stat() function is used to return the file info structure describing the file. Let's first use the Stat() function only and see whether it will be enough to detect the presence of a file in Go.Example 1Consider the code shown below.package main import(    "fmt"    "os" ) func main() {    if _, err := os.Stat("sample.txt"); err == nil {       fmt.Printf("File ... Read More

How to handle errors within WaitGroups in Golang?

Mukul Latiyan
Updated on 22-Feb-2022 06:58:49

3K+ Views

There are chances that we might get some panic while running multiple goroutines. To deal with such a scenario, we can use a combination of channel and waitgroups to handle the error successfully and not to exit the process.Let's suppose there's a function that when invoked returns a panic, which will automatically kill the execution of the program, as when panic gets called it internally calls os.Exit() function. We want to make sure that this panic doesn't close the program, and for that, we will create a channel that will store the error and then we can use that later ... Read More

How to wait for a goroutine to finish in Golang?

Mukul Latiyan
Updated on 22-Feb-2022 05:46:39

7K+ Views

We know that goroutines can be a bit tricky at first, and often we find cases where the main goroutines will exit without giving a chance to the inside goroutines to execute.In order to be able to run the goroutines until the finish, we can either make use of a channel that will act as a blocker or we can use waitGroups that Go's sync package provides us with.Let's first explore a case where we have a single goroutines that we want to finish and then do some other work.Example 1Consider the code shown below.package main import (    "fmt" ... Read More

Advertisements