All Go Rust Python PHP JavaScript
Chrome Dev Summit to secure your spot in workshops, office hours and learning lounges!

How to Read a File Line-by-Line in Go

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...
Sambhav Khandelwal

How to Remove Duplicates Strings from Slice in Go

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...
pooriabt

How To Install Go in Linux

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...
Unused

How to find the type of an object in Go

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...
Patcher56

How to get the first and last day of the current month in Go

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...
Tomoki

How to format a date or time in Go

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...
aweis

How to check if a map is empty in Go

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"...
Tomoki

How to convert an int to a string type in Go

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...
aweis

How to delete an element from a slice in Go

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...
pooriabt

How to shuffle a slice in Go

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"} ...
ada

How to reverse a slice in Go

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)/...
ada

How to split a string into a slice in Go

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...
Unused

How to join a slice of strings into a single string in Go

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...
Tomoki

How to find intersection of two slices in Go

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...
Patcher56

How to fill a slice with values in Go

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...
pooriabt