Comparing two slices in Go.Using map cache
Creates a slice of slice values not included in the other given slices. An implementation is shown in the section below.
package main
import "fmt"
// difference returns the elements in `a` that aren't in ...
Slice chunking in Go.Using for Loop
The easiest method involves iterating over the slice and incrementing by the chunk size. An implementation is shown in the section below.
package main
import "fmt"
func chunkSlice(slice []int, chunkSize 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...
Reverse a String in Golang.Using range
a range on a string returns a rune which is a codepoint.
package main
import "fmt"
func Reverse(s string) (result string) {
for _, v := range s {
result = string(v) + result
}
return
}
func main() ...
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...
Extracting substrings in Go.Using the slice syntax
A slice is formed by specifying two indices, a low and high bound, separated by a colon:
// a[low : high]
s := "Hello,世界"
// e
fmt.Println(s[1:2])
fmt.Println(s[1:])
// Hello,世界
fmt.Println(s[:])
...
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 ...
Reading from a url resource in Go.Fetching a URL
Go provides a collection of packages, grouped under net, that make it easy to send and receive information through the Internet. Package http provides HTTP client and server implementations.
packag...
Format a string without printing in Go.Basics
Package fmt implements formatted I/O with functions analogous to C's printf and scanf. The format 'verbs' are derived from C's but are simpler.
fmt.Sprintf
Sprintf formats according to a format specif...
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 ...
Go test string contains substring.Using strings.Contains Function
How do I check if a string is a substring of another string in Go? Use the function Contains from the strings package.
Contains function returns a boolean value. It returns true if the...
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 ...
Type casting an interface to a string in go.Using fmt.Sprintf Function
To convert interface to string in Go, use fmt.Sprintf function.
Sprintf formats according to a format specifier and returns the resulting string.
package main
import "fmt"
var ...
Hello, World! is the first basic program in any programming language.source code
package main
import "fmt"
func main() {
fmt.Println("Hello,世界,こんにちは,안녕하세요")
}
To run the program
Go is a compiled language. The Go toolchain converts a source pro...