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

How to Add Two Numbers in Go

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

How to Marshal / Unmarshal Json with a Custom Type Attribute in Go

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

How to Subtracting time.Duration from Time in Go

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

How to Parse Unix Timestamp to time.Time in Go

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

How to Get the Maximum Value for an Int Type in Go

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

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

How to use range in Go

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

How to generate a random string in Go

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

How to format byte size as kilobytes, megabytes in Go

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

How many keywords are there in Go

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

How to convert string to json in Go

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

How to convert interface to int64 in Go

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

How to Convert string to integer type in Go

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