An example program using numbers.
Integers And Floating Point Numbers
Go has several different types to represent numbers. Generally we split numbers into two different kinds: integers and floating-point numbers.
Make a main.go file containing the ...
In Golang, using the Unmarshal JSON function is the easiest way to Marshal / Unmarshal json with a custom type attribute.
JSON (JavaScript Object Notation) is a lightweight data-interchange format.
Using UnmarshalJSON Function
Example on custom Unm...
In Golang, using the time.Duration function is the easiest way to subtract time from a time.Time
Using time.Duration Function
A Duration represents the elapsed time between two instants as an int64 nanosecond count. The representation limits the la...
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 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 (
"...
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...
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...
Range loop (for-each) patterns.For-each Loop (slice or map)
The range form of the for loop iterates over a slice or map.
When ranging over a slice, two values are returned for each iteration. The first is the index, and the second is a copy of the el...
Creating Random Strings of a fixed length in Go.
Using rand.Int63 Function
Int63 returns a non-negative pseudo-random 63-bit integer as an int64 from the default Source.
package main
import (
"fmt"
"math/rand"
"time"
)
const letters = "a...
Convert File Size to a human friendly format.Get file size in human-readable units like kilobytes (KB), Megabytes (MB) or GigaBytes (GB)
SI (Decimal 1 k = 1,000)
A metric prefix is a unit prefix that precedes a basic unit of measure to indicate a mul...
List of Golang Keywords.
Keywords
The following keywords are reserved and may not be used as identifiers.
break default func interface select
case defer go map struct
chan else goto package switch
const ...
Converting Json to string in Golang.Decoding JSON Into Structs
Go offers built-in support for JSON encoding and decoding, including to and from built-in and custom data types.
Unmarshal parses the JSON-encoded data and stores the result in the value ...
Interface variable conversion in Golang.Type Assertion
To convert interface to int64 in Go, Using Type Assertion.
A type assertion provides access to an interface value's underlying concrete value.
package main
import "fmt"
// t := i.(T)
// t, ok ...
The most common numeric conversions are Atoi.strconv.Atoi Function
Package strconv implements conversions to and from string representations of basic data types.
package main
import (
"fmt"
"strconv"
)
func main() {
i, err := strconv.Atoi("...