In Golang, there are 2 ways to read a file line-by-line.
Using bufio.Scanner Function
Scanner provides a convenient interface for reading data such as a file of newline-delimited lines of text. The ones that are currently implemented are:
packa...
In Golang, there are 2 ways to remove duplicates strings from slice.Using a Hash Map
We have defined a function where we are passing the slice values and using the map function we are checking the duplicates and removing them. The ones that are curr...
In Golang, there are 2 ways to install Go in Linux.Download Packages
Download packages for Windows 64-bit, macOS, Linux, and more.
Go install
Linux
Remove any previous Go installation by deleting the /usr/local/go folder (if it exists), then extr...
In Golang, there are three ways to find the type of an object.Using fmt.Sprintf Function
You can use fmt.Sprintf function to get a string representation. For example,
package main
import "fmt"
func main() {
i := 10
f := 2.0
var m map[s...
In Golang, there are two ways to get the start and end date of the current month.Using time.AddDate Function
AddDate returns the time corresponding to adding the given number of years, months, and days to t. For example,
package main
import (
"fm...
In Golang, there are two ways to format the time.The layout string
Go doesn’t use yyyy-mm-dd layout to format a time. Instead, you format a special layout parameter.
2006-01-02 15:04:05.999999999 -0700 MST
package main
import (
"fmt"
"tim...
2 ways to check if a map is empty in Golang.Using len Function
You can use the len function to check if a map is empty. it returns the length of v, according to its type.
len(s) map[K]T: map length (number of defined keys)
package main
import "fmt"...
3 ways to convert int64 to string in Golang.Using strconv.Itoa Function
The code snippet below shows how to convert an int to a string using the Itoa function.
package main
import (
"fmt"
"strconv"
)
func main() {
s := strconv.Itoa(100)
f...
Removing a string from a slice in Golang.Faster Version
If you do not care about ordering, you have the much faster possibility to replace the element to delete with the one at the end of the slice and then return the n-1 first elements:
package mai...
Shuffling a Slice in Golang.Using rand.Shuffle Function
Shuffle pseudo-randomizes the order of elements using the default Source.
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
s := []string{"Japan", "Germany", "France"}
...
Reverse a Slice in Golang.Using a for Loop
The standard library does not have a built-in function for reversing a slice. Use a for loop to reverse a slice:
package main
import "fmt"
func main() {
s := []string{"b", "c", "d"}
for i := len(s)/...
Splitting a String into a Slice in Golang.Using strings.Split Function
Use the strings.Split function to split a string into its comma separated values.
package main
import (
"fmt"
"strings"
)
func main() {
s := "Japan,Germany"
// func S...
Convert Slice to String in Golang.Using strings.Join Function
Converting a slice of strings to a single string is pretty easy to do in Go. The strings.Join() method is present in the standard library for this purpose, and its usage is shown below:
p...
Intersection set of golang slices.Using map cache
It's a best method for intersection two slice. Time complexity is too low. Time Complexity : O(m+n)
package main
import "fmt"
func intersection(a, b []string) ([]string, error) {
// uses empty s...
Filling a slice with a repeated pattern.Using for Loop
Using a for loop is the simplest solution. Fill the slice with the value 8 by looping through each element and setting the value.
package main
import "fmt"
func main() {
slice := make([]int...