Tech Archives - Welcome To Golang By Example https://vikasboss.github.io/category/tech/ Sun, 10 Dec 2023 16:12:09 +0000 en-US hourly 1 https://wordpress.org/?v=6.8.1 https://i0.wp.com/golangbyexamples.com/wp-content/uploads/2021/05/cropped-go_border-1.png?fit=32%2C32&ssl=1 Tech Archives - Welcome To Golang By Example https://vikasboss.github.io/category/tech/ 32 32 159787465 Check all groups that current logged in user belong to on Linux https://vikasboss.github.io/current-log-user-groups/ https://vikasboss.github.io/current-log-user-groups/#respond Sun, 10 Dec 2023 16:12:07 +0000 https://vikasboss.github.io/?p=14244 groups command on linux can be used to know which all groups a user belongs to Just type in groups on the terminal. Below is the output on my Mac machine

The post Check all groups that current logged in user belong to on Linux appeared first on Welcome To Golang By Example.

]]>
groups command on linux can be used to know which all groups a user belongs to

Just type in groups on the terminal. Below is the output on my Mac machine

staff everyone localaccounts _appserverusr admin

The post Check all groups that current logged in user belong to on Linux appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/current-log-user-groups/feed/ 0 14244
Ruby convert string to bool https://vikasboss.github.io/string-bool-ruby/ https://vikasboss.github.io/string-bool-ruby/#respond Thu, 24 Aug 2023 07:26:30 +0000 https://vikasboss.github.io/?p=12978 Overview In the Ruby language, strings “true” and “false” are interpreted as true when used in the if condition. See example below Output “false” evaluates to true Therefore it becomes important to...

The post Ruby convert string to bool appeared first on Welcome To Golang By Example.

]]>
Overview

In the Ruby language, strings “true” and “false” are interpreted as true when used in the if condition. See example below

if "false"
   puts "yes"
end

Output

"yes"

“false” evaluates to true

Therefore it becomes important to handle string “false” or string “true” correctly.

We can create a custom method that can return boolean true or false based on the content of the string

def true?(str)
  str.to_s.downcase == "true"
end

We can try out the above function

true?("false")
 => false
true?("true")
 => true

Also, note that for strings other than “false” it will give true in return, but the function could be easily modified to handle that case

The post Ruby convert string to bool appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/string-bool-ruby/feed/ 0 12978
Know the current user on Linux https://vikasboss.github.io/know-the-current-user-on-linux/ https://vikasboss.github.io/know-the-current-user-on-linux/#respond Wed, 09 Aug 2023 13:14:38 +0000 https://vikasboss.github.io/?p=12839 Overview Linux command ‘whoami’ can be used to know the currently logged-in user. Let’s see this command in action. Go to the terminal and type in the command It will print the...

The post Know the current user on Linux appeared first on Welcome To Golang By Example.

]]>
Overview

Linux command ‘whoami’ can be used to know the currently logged-in user.

Let’s see this command in action. Go to the terminal and type in the command

whoami

It will print the current user. Let’s say the current user is john then running the above command will simply print john

Output

whoami
john

The post Know the current user on Linux appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/know-the-current-user-on-linux/feed/ 0 12839
Convert an array of int or numbers to string in Go (Golang) https://vikasboss.github.io/array-int-string-golang/ https://vikasboss.github.io/array-int-string-golang/#respond Mon, 06 Mar 2023 17:39:47 +0000 https://vikasboss.github.io/?p=11347 Overview In this tutorial, we will see how to convert an array of ints or numbers to a string in Go. Below is the program for the same. Program Here is the...

The post Convert an array of int or numbers to string in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
Overview

In this tutorial, we will see how to convert an array of ints or numbers to a string in Go.

Below is the program for the same.

Program

Here is the program for the same.

package main

import (
	"fmt"
	"strconv"
)

func main() {
	sample := []int{2, 1, 3}

	output := ""
	for _, v := range sample {
		temp := strconv.Itoa(v)
		output = output + temp
	}
	fmt.Println(output)
}

Output

213

Note: Check out our Golang Advanced Tutorial. The tutorials in this series are elaborative and we have tried to cover all concepts with examples. This tutorial is for those who are looking to gain expertise and a solid understanding of golang – Golang Advance Tutorial

Also if you are interested in understanding how all design patterns can be implemented in Golang. If yes, then this post is for you –

All Design Patterns Golang

Note: Check out our system design tutorial series System Design Questions

The post Convert an array of int or numbers to string in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/array-int-string-golang/feed/ 0 11347
Check if a string contains single or multiple whitespaces in Go (Golang) https://vikasboss.github.io/string-whitespace-golang/ https://vikasboss.github.io/string-whitespace-golang/#respond Fri, 10 Feb 2023 14:55:10 +0000 https://vikasboss.github.io/?p=10985 Overview A simple regex can be used to check if a string contains single or multiple white spaces in Go. Here is the program for the same Program Output Note: Check out...

The post Check if a string contains single or multiple whitespaces in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
Overview

A simple regex can be used to check if a string contains single or multiple white spaces in Go.

Here is the program for the same

Program

package main

import (
	"fmt"
	"regexp"
)

func main() {
	//Single whitespace
	sampleWord := "Good morning"

	isWhitespacePresent := regexp.MustCompile(`\s`).MatchString(sampleWord)
	fmt.Println(isWhitespacePresent)

	//Multiple Whitespace
	sampleWord = "Good   morning"
	isMultipleWhitespacePresent := regexp.MustCompile(`\s*`).MatchString(sampleWord)
	fmt.Println(isMultipleWhitespacePresent)
}

Output

true
true

Note: Check out our Golang Advanced Tutorial. The tutorials in this series are elaborative and we have tried to cover all concepts with examples. This tutorial is for those who are looking to gain expertise and a solid understanding of golang – Golang Advance Tutorial

Also if you are interested in understanding how all design patterns can be implemented in Golang. If yes, then this post is for you –

All Design Patterns Golang

Note: Check out our system design tutorial series System Design Questions

The post Check if a string contains single or multiple whitespaces in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/string-whitespace-golang/feed/ 0 10985
Create or initialize a new string in Go (Golang) https://vikasboss.github.io/create-string-golang/ https://vikasboss.github.io/create-string-golang/#respond Tue, 07 Feb 2023 08:48:04 +0000 https://vikasboss.github.io/?p=10933 Overview Below is a simple way to initialize or create a string in Go. In the below program, we have simply declared as well as defined a string named sample Notice the...

The post Create or initialize a new string in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
Overview

Below is a simple way to initialize or create a string in Go. In the below program, we have simply declared as well as defined a string named sample

Notice the syntax

sample := "test"

Here is the full program

Program

package main

import "fmt"

func main() {
	sample := "test"
	fmt.Println(sample)
}

Output

test

If we want to only declare a string variable then below is the way. A declared string is initialized to an empty string

package main

import "fmt"

func main() {
    var sample string
    fmt.Println(sample)
}

Output

Note: Check out our Golang Advanced Tutorial. The tutorials in this series are elaborative and we have tried to cover all concepts with examples. This tutorial is for those who are looking to gain expertise and a solid understanding of golang – Golang Advance Tutorial

Also if you are interested in understanding how all design patterns can be implemented in Golang. If yes, then this post is for you –

All Design Patterns Golang

Note: Check out our system design tutorial series System Design Questions

The post Create or initialize a new string in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/create-string-golang/feed/ 0 10933
Delete or Remove a key from a map in Go (Golang) https://vikasboss.github.io/delete-key-map-golang/ https://vikasboss.github.io/delete-key-map-golang/#respond Mon, 02 Jan 2023 14:41:04 +0000 https://vikasboss.github.io/?p=10495 Overview Below is the format to delete a given key from a map There are two cases Let’s see an example of both the cases Key exists in the Map Below is...

The post Delete or Remove a key from a map in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
Overview

Below is the format to delete a given key from a map

delete(mapName, keyToDelete)

There are two cases

  • The keyToDelete exists in the map. In this case, it will simply delete the key
  • The keyToDelete doesn’t exist in the map. In this case, it will do nothing. A

Let’s see an example of both the cases

Key exists in the Map

Below is the program for the case where the key exists in the Map

package main

import "fmt"

func main() {
	sample := make(map[string]int)
	sample["a"] = 1
	sample["b"] = 2
	sample["c"] = 3

        fmt.Println(sample)
	delete(sample, "a")

	fmt.Println(sample)
}

Output

map[a:1 b:2 c:3]
map[b:2 c:3]

Notice in the output that the key “a” got deleted from the map. It is always a good practice to check if a key exists in the map and then delete it. Below is the code snippet for that.

package main

import "fmt"

func main() {
	sample := make(map[string]int)
	sample["a"] = 1
	sample["b"] = 2
	sample["c"] = 3

	fmt.Println(sample)

	if _, ok := sample["a"]; ok {
		delete(sample, "a")
	}

	fmt.Println(sample)
}

Output

map[a:1 b:2 c:3]
map[b:2 c:3]

The Key doesn’t exist in the Map

In this case, also it is a good practice to check if the key exists and then delete it. Even if we delete directly without checking, then also it is not a problem though. The below code snippet shows both the cases

package main

import "fmt"

func main() {
	sample := make(map[string]int)
	sample["a"] = 1
	sample["b"] = 2
	sample["c"] = 3

	fmt.Println(sample)

	//Check and delete
	if _, ok := sample["d"]; ok {
		delete(sample, "d")
	}

	fmt.Println(sample)

	//Directly delete
	delete(sample, "d")

	fmt.Println(sample)
}

Output

map[a:1 b:2 c:3]
map[a:1 b:2 c:3]
map[a:1 b:2 c:3]

Note: Check out our Golang Advanced Tutorial. The tutorials in this series are elaborative and we have tried to cover all concepts with examples. This tutorial is for those who are looking to gain expertise and a solid understanding of golang – Golang Advance Tutorial

Also if you are interested in understanding how all design patterns can be implemented in Golang. If yes, then this post is for you –

All Design Patterns Golang

The post Delete or Remove a key from a map in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/delete-key-map-golang/feed/ 0 10495
Convert an IOTA or Enum to a string in Go (Golang) https://vikasboss.github.io/convert-an-iota-or-enum-to-a-string-in-go-golang/ https://vikasboss.github.io/convert-an-iota-or-enum-to-a-string-in-go-golang/#respond Thu, 10 Nov 2022 13:51:54 +0000 https://vikasboss.github.io/?p=9986 Overview Enum in Golang can be created by using IOTA. Please refer to this post to learn more about IOTA In this post, we will see how to convert an IOTA or...

The post Convert an IOTA or Enum to a string in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
Overview

Enum in Golang can be created by using IOTA. Please refer to this post to learn more about IOTA

In this post, we will see how to convert an IOTA or enum to a string value. By default when you will print an IOTA or enum value it will print the integer part of it. See this example. Later we will see an example of we can define a custom toString method to print the string value of IOTA or enum

Example

package main

import "fmt"

type Size uint8

const (
	small Size = iota
	medium
	large
	extraLarge
)

func main() {
	fmt.Println(small)
	fmt.Println(medium)
	fmt.Println(large)
	fmt.Println(extraLarge)
}

Output

0
1
2
3

We can also define a method toString on Size type to print the exact value of enum . See below program

package main
import "fmt"
type Size int
const (
    small = Size(iota)
    medium
    large
    extraLarge
)
func main() {
    var m Size = 1
    m.toString()
}
func (s Size) toString() {
    switch s {
    case small:
        fmt.Println("Small")
    case medium:
        fmt.Println("Medium")
    case large:
        fmt.Println("Large")
    case extraLarge:
        fmt.Println("Extra Large")
    default:
        fmt.Println("Invalid Size entry")
    }
}

Output

medium

We will now define a toString method for the Size type. It can be used to the print the string value of the constant of Size type.

The post Convert an IOTA or Enum to a string in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/convert-an-iota-or-enum-to-a-string-in-go-golang/feed/ 0 9986
Enum in Golang https://vikasboss.github.io/enum-in-golang/ https://vikasboss.github.io/enum-in-golang/#respond Thu, 10 Nov 2022 13:44:03 +0000 https://vikasboss.github.io/?p=9981 Overview IOTA provides an automated way to create an enum in Golang. Let’s see an example. Example Output In above program we created a new type Then we declared some const of ...

The post <strong>Enum in Golang</strong> appeared first on Welcome To Golang By Example.

]]>
Overview

IOTA provides an automated way to create an enum in Golang. Let’s see an example.

Example

package main

import "fmt"

type Size uint8

const (
	small Size = iota
	medium
	large
	extraLarge
)

func main() {
	fmt.Println(small)
	fmt.Println(medium)
	fmt.Println(large)
	fmt.Println(extraLarge)
}

Output

0
1
2
3

In above program we created a new type

type Size uint8

Then we declared some const of  type Size. The first constant small is set to iota so it will be set to zero

small Size = iota

That’s why

fmt.Println(small)      >> outputs 0  
fmt.Println(medium)     >> outputs 1
fmt.Println(large)      >> outputs 2
fmt.Println(extraLarge) >> outputs 3

Without IOTA we had to explicitly set the values of each of the enum value

package main

import "fmt"

type Size uint8

const (
	small      Size = 0
	medium     Size = 1
	large      Size = 2
	extraLarge Size = 3
)

func main() {
	fmt.Println(small)
	fmt.Println(medium)
	fmt.Println(large)
	fmt.Println(extraLarge)
}

Output

0
1
2
3

We can also define a method toString on Size type to print the exact value of enum . See below program

package main
import "fmt"
type Size int
const (
    small = Size(iota)
    medium
    large
    extraLarge
)
func main() {
    var m Size = 1
    m.toString()
}
func (s Size) toString() {
    switch s {
    case small:
        fmt.Println("Small")
    case medium:
        fmt.Println("Medium")
    case large:
        fmt.Println("Large")
    case extraLarge:
        fmt.Println("Extra Large")
    default:
        fmt.Println("Invalid Size entry")
    }
}

Output

medium

We have defined a toString method for the Size type. It can be used to the print the string value of the constant of Size type.

The post <strong>Enum in Golang</strong> appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/enum-in-golang/feed/ 0 9981
Sort a slice of Int in Ascending and Descending order in Go (Golang) https://vikasboss.github.io/sort-slice-asc-desc-golang/ https://vikasboss.github.io/sort-slice-asc-desc-golang/#respond Thu, 13 Oct 2022 17:13:49 +0000 https://vikasboss.github.io/?p=9648 Sort a slice in Ascending order sort.Ints package of going can be used to sort a full slice or a part of the slice. It is going to sort the string into...

The post Sort a slice of Int in Ascending and Descending order in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
Sort a slice in Ascending order

sort.Ints package of going can be used to sort a full slice or a part of the slice. It is going to sort the string into Ascending order.

Below is the signature of the method

https://pkg.go.dev/sort#Ints

func Ints(x []int)

It takes a slice ‘x’ as an argument and sorts the slice ‘x’ in place.

Below is the program for the same

package main

import (
	"fmt"
	"sort"
)

func main() {

	nums := []int{3, 4, 2, 1}
	fmt.Printf("Before: %v", nums)
	sort.Ints(nums)
	fmt.Printf("\nAfter: %v", nums)
}

Output

Before: [3 4 2 1]
After: [1 2 3 4]

Sort a slice in Descending order

To sort a slice in descending order we will use Slice method of sort pacakge

https://pkg.go.dev/sort#Slice

Below is the signature of the method

func Slice(x any, less func(i, j int) bool)

So this function takes two arguments

  • less func(i, j int) – This function is nothing but a comparator function

We can use this comparator function to decide the descending order of the elements in the slice. Below is an example

package main

import (
	"fmt"
	"sort"
)

func main() {

	nums := []int{3, 4, 2, 1}
	fmt.Printf("Before: %v", nums)
	sort.Slice(nums, func(i, j int) bool {
		return nums[i] > nums[j]
	})
	fmt.Printf("\nAfter: %v", nums)
}

Output

Before: [3 4 2 1]
After: [4 3 2 1]

As a matter of fact, you can use sort.Slice method to also sort a slice in descending order. Below is an example

package main

import (
	"fmt"
	"sort"
)

func main() {

	nums := []int{3, 4, 2, 1}
	fmt.Printf("Before: %v", nums)
	sort.Slice(nums, func(i, j int) bool {
		return nums[i] < nums[j]
	})
	fmt.Printf("\nAfter: %v", nums)
}

Output

Before: [3 4 2 1]
After: [1 2 3 4]

Note: Check out our Golang Advanced Tutorial. The tutorials in this series are elaborative and we have tried to cover all concepts with examples. This tutorial is for those who are looking to gain expertise and a solid understanding of golang - Golang Advance Tutorial

Also if you are interested in understanding how all design patterns can be implemented in Golang. If yes, then this post is for you -

All Design Patterns Golang

Note: Check out our system design tutorial series System Design Questions

The post Sort a slice of Int in Ascending and Descending order in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/sort-slice-asc-desc-golang/feed/ 0 9648