In Golang, using the time.Unix() function is the easiest way to parse unix timestamp to time.Time
Using time.Unix Function
You can directly use time.Unix function of time which converts the unix time stamp to UTC.
See the following example:
pa...
In Golang, there are 3 ways to trim leading and trailing white spaces of a string.
Using strings.TrimSpace Function
The easiest way to trim leading and trailing white spaces of a string in Golang. For example,
package main
import (
"fmt"
"...
In Golang, there are 2 ways to get the maximum value for an int type.
Using constant values
Since integer types use two's complement arithmetic, you can infer the min/max constant values for int and uint. For example,
package main
import (
"...
In Golang, there are 2 ways to list all standard packages.
Using packages
go install
Use the following command to download the repository to the local file system.
install
go get golang.org/x/tools/go/packages
go install golang.or...
In Golang, there are 3 ways to read a whole file into a string variable.
Using ioutil.ReadFile Function
ReadFile reads the file named by filename and returns the contents. A successful call returns err == nil, not err == EOF. Because ReadFile reads...
In Golang, there are 3 ways to check the equality of two slices.
Using bytes.Equal Function
Equal reports whether a and b are the same length and contain the same bytes. A nil argument is equivalent to an empty slice.
For example,
package main...
In Golang, using the delete function is the easiest way to delete keys in a map.
Using delete Function
Syntax
The syntax of the delete function is shown below.
func delete(m map[Type]Type1, key Type)
delete Usage
The delete built-in funct...
In Golang, there are 3 ways to get the directory of the currently running file.Using os.Executable Function
Executable returns the path name for the executable that started the current process.
The easiest way to get the directory of the currently ...
In Golang, there are 2 ways to pretty-print json.
Using json.MarshalIndent Function
MarshalIndent is like Marshal but applies Indent to format the output. Each JSON element in the output will begin on a new line beginning with prefix followed by one...
In Golang, there are 3 ways to remove packages installed with go get.
Using go mod tidy
go mod tidy ensures that the go.mod file matches the source code in the module. It adds any missing module requirements necessary to build the current module’s p...
In Golang, there are 3 ways to get a slice of keys from a map.Using For Range
The easiest way to get keys of map in Golang:
package main
import "fmt"
func main() {
m := map[string]int{
"a": 1,
"b": 1,
"c": 1,
}
// minimize ...
In Golang, there are 2 ways to assign string to bytes.
Using byte Function
The easiest way to convert string to []byte in Golang:
package main
import "fmt"
func main() {
s := "Hello"
b := []byte(s)
fmt.Printf("%v\n", b)
}
[72 101 10...
In Golang, there are 2 ways to convert byte array to string.Using string Function
The easiest way to convert []byte to string in Golang:
package main
import "fmt"
func main() {
b := []byte{'a', 'b', 'c'}
s := string(b[:])
fmt.Printf("%...
In Golang, there are 2 ways to concatenate two slices.Using append Function
The append built-in function appends elements to the end of a slice.
You can use append function, it really simple. For example,
package main
import "fmt"
func main(...
In Golang, there are 2 ways to check if a file exists.Using os.Stat Function
Stat returns a FileInfo describing the named file. See the following example:
package main
import (
"errors"
"fmt"
"os"
)
func main() {
// since the additio...