Siva Sai

Siva Sai

222 Articles Published

Articles by Siva Sai

Page 22 of 23

How to Assign Default Value for Struct Field in Golang?

Siva Sai
Siva Sai
Updated on 19-Apr-2023 23K+ Views

In Golang, it is common to have struct types that need to be initialized with default values. In this article, we will explore how to assign a default value for a struct field in Golang. Assigning Default Values for Struct Fields To assign a default value for a struct field in Golang, we can define a default value for the field during the struct type declaration. For example, consider the following struct type − type Person struct { Name string Age int Address string ...

Read More

How to append a slice in Golang?

Siva Sai
Siva Sai
Updated on 19-Apr-2023 7K+ Views

Appending a slice in Golang is a common operation when working with collections of data. In this article, we will explore how to append a slice in Golang. Syntax for Appending a Slice in Golang slice = append(slice, element) The append() function in Golang appends the given element(s) to the end of a slice and returns a new slice. If the original slice has enough capacity to hold the new elements, the underlying array is reused, else a new array is allocated. Example Here's an example of how to append a slice in Golang. package main import ( ...

Read More

How Many Logical Processors Used By Current Process in Golang

Siva Sai
Siva Sai
Updated on 19-Apr-2023 851 Views

In modern computing, multi-core processors are ubiquitous, which can execute multiple tasks simultaneously. In Golang, we can take advantage of this by using concurrency to perform tasks in parallel. However, how can we determine the number of logical processors used by the current process? In this article, we will discuss how to find out the number of logical processors used by the current process in Golang. Using the Runtime Package The Go programming language provides the runtime package, which includes functions that allow us to control and interact with the Go runtime environment. One of these functions is runtime.NumCPU(), which ...

Read More

Higher-Order Function in Golang

Siva Sai
Siva Sai
Updated on 19-Apr-2023 1K+ Views

Higher-order functions are a powerful feature of modern programming languages, including Golang. In this article, we will discuss what higher-order functions are and how they can be used in Golang. What is a Higher-Order Function? A higher-order function is a function that takes one or more functions as arguments and/or returns a function as its result. This means that higher-order functions can be used to create new functions that are tailored to specific needs, making code more efficient and reusable. Higher-Order Function Example in Golang Let's consider a simple example to understand how higher-order functions work in Golang. Suppose we ...

Read More

Golang Program to Check if the String is Alphanumeric

Siva Sai
Siva Sai
Updated on 19-Apr-2023 3K+ Views

In Go programming language, it is essential to check if a string contains alphanumeric characters or not. Alphanumeric characters are a combination of alphabets and numbers, and they are commonly used in passwords, usernames, and other important data. In this article, we will discuss how to write a Golang program to check if a string is alphanumeric. What is an Alphanumeric String? An alphanumeric string is a string that contains a combination of alphabets and numbers. It can also include special characters such as underscore (_) and dash (-), but not all special characters are considered alphanumeric. Alphanumeric strings are ...

Read More

Golang program that removes duplicates, ignores order

Siva Sai
Siva Sai
Updated on 18-Apr-2023 233 Views

When working with slices in Golang, it's common to need to remove duplicate elements from the slice. While there are many ways to do this, one approach that can be particularly useful is to remove duplicates while ignoring the order of the elements. This can be useful, for example, when you want to compare two slices for equality without caring about the order of the elements. In this article, we'll explore a simple Golang program that removes duplicates from a slice while ignoring the order of the elements. We'll break down the program step by step to explain how it ...

Read More

Golang program that uses structs as map keys

Siva Sai
Siva Sai
Updated on 18-Apr-2023 6K+ Views

Golang is a powerful programming language that provides a variety of features to work with structured data. One of these features is the ability to use structs as keys in maps. In this article, we will explore how to write a Golang program that uses structs as map keys. What is a Struct? In Golang, a struct is a composite data type that groups together zero or more values of various types. Structs are used to create more complex data types that can represent a range of objects. A struct is defined using the type and struct keywords, followed by ...

Read More

Golang Program that uses String Switch

Siva Sai
Siva Sai
Updated on 18-Apr-2023 2K+ Views

Golang is a popular programming language that offers a range of powerful features. One of these features is the ability to use the switch statement with a string type variable. In this article, we will explore how to write a Golang program that uses a string switch statement. What is a String Switch? In Golang, the switch statement can be used to test a variable against a list of values. A string switch statement is a type of switch statement that is specifically designed to work with string variables. A string switch statement works by comparing the value of a ...

Read More

Golang Program that Uses Named Return Values and Defaults

Siva Sai
Siva Sai
Updated on 18-Apr-2023 906 Views

Golang is a powerful programming language that offers a range of features and tools for building efficient and reliable applications. One such feature is the ability to use named return values and defaults in functions. In this article, we will discuss how to use named return values and defaults in Golang and why this feature is useful. What are Named Return Values and Defaults in Golang? In Golang, you can define named return values in a function signature, which can help make your code more readable and maintainable. Named return values allow you to define the names and types of ...

Read More

Golang Program that uses func as Local Variable

Siva Sai
Siva Sai
Updated on 18-Apr-2023 457 Views

Golang is a popular programming language that offers a range of features and tools for building efficient and reliable applications. One such feature is the ability to use functions as local variables. In this article, we will discuss how to use functions as local variables in Golang and why this feature is useful. What are Local Variables in Golang? Before we dive into how to use functions as local variables in Golang, let's first understand what local variables are. In programming, a local variable is a variable that is defined within a function or block of code. Local variables are ...

Read More
Showing 211–220 of 222 articles
Advertisements