The post Check all groups that current logged in user belong to on Linux appeared first on Welcome To Golang By Example.
]]>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.
]]>The post Ruby convert string to bool appeared first on Welcome To Golang By Example.
]]>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.
]]>The post Know the current user on Linux appeared first on Welcome To Golang By Example.
]]>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.
]]>The post Convert an array of int or numbers to string in Go (Golang) appeared first on Welcome To Golang By Example.
]]>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.
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 –
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.
]]>The post Check if a string contains single or multiple whitespaces in Go (Golang) appeared first on Welcome To Golang By Example.
]]>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
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 –
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.
]]>The post Create or initialize a new string in Go (Golang) appeared first on Welcome To Golang By Example.
]]>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
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 –
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.
]]>The post Delete or Remove a key from a map in Go (Golang) appeared first on Welcome To Golang By Example.
]]>Below is the format to delete a given key from a map
delete(mapName, keyToDelete)
There are two cases
Let’s see an example of both the cases
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]
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 –
The post Delete or Remove a key from a map in Go (Golang) appeared first on Welcome To Golang By Example.
]]>The post Convert an IOTA or Enum to a string in Go (Golang) appeared first on Welcome To Golang By Example.
]]>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
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.
]]>The post <strong>Enum in Golang</strong> appeared first on Welcome To Golang By Example.
]]>IOTA provides an automated way to create an enum in Golang. Let’s see an 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.
]]>The post Sort a slice of Int in Ascending and Descending order in Go (Golang) appeared first on Welcome To Golang By Example.
]]>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
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]
To sort a slice in descending order we will use Slice method of sort pacakge
Below is the signature of the method
func Slice(x any, less func(i, j int) bool)
So this function takes two arguments
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 -
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.
]]>