Catching return values from goroutines in Go.
Channels
A channel is a communication mechanism that lets one goroutine send values to another goroutine.
Make a main.go file containing the following:
package main
import (
"fmt"
"time"
)
func ...
Variadic Functions Tutorial with Examples in Go.
Variadic Function
A variadic function is one that can be called with varying numbers of arguments. The most familiar examples are fmt.Printf and its variants.
package main
import "fmt"
func add(v...
Testing the equivalence of maps in Go.
reflect.DeepEqual Function
It first checks if both maps are nil, then it checks if they have the same length before finally checking to see if they have the same set of (key, value) pairs.
package main
impo...
Golang bytes.Buffer Examples
Examples of Golang bytes.Buffer
The bytes package provides the Buffer type for efficient manipulation of byte slices. A Buffer starts out empty but grows as data of types like string, byte, and []byte are written to it.
...
Formatting float to currency string in Go.
Using localized formatting
Use golang.org/x/text/message to print using localized formatting for any language in the Unicode CLDR.
Make a main.go file containing the following:
package main
import (
"...
Golang path and filepath Examples.
Using filepath.Base
The number is the index of the last slash in the string. If you want to get the file's base name, use filepath.Base:
package main
import (
"fmt"
"path/filepath"
)
func main() {
path ...
Understanding Rune in Go.
UTF-8 Decoder
The unicode/utf8 package provides one that we can use like this:
package main
import (
"fmt"
"unicode/utf8"
)
func main() {
s := "Hello, 世界"
for i := 0; i < len(s); {
r, size := utf8.DecodeR...
Introduction to Strings in Go.What is a String?
A string is an immutable sequence of bytes. Strings may contain arbitrary data, including bytes with value 0, but usually they contain human-readable text.
Make a main.go file containing the following...
In Golang, there are 2 ways to check if a slice contains an element.
Using contains Function
The following example should cover whatever you are trying to do:
package main
import "fmt"
func Contains[E comparable](s []E, v E) bool {
for _, ...
In Golang, using the sort.Slice function is the easiest way to sort a slice
Using Slice Function
You simply pass an anonymous function to the sort.Slice function:
package main
import (
"fmt"
"sort"
)
func main() {
s := []string{"Houston...
In Golang, there are 2 ways to check string is in json format.
Using Unmarshal Function
The following example should cover whatever you are trying to do:
package main
import (
"encoding/json"
"fmt"
)
func isJson(s string) bool {
var js ...
In Python, there are 2 ways to delete a character from a string.
Using replace Method
The str.replace(old, new[, count]) method returns a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is giv...
In Python, there are 2 ways to make a list of alphabet characters.
Using string Module
The string.ascii_lowercase is a pre-initialized string used as string constant. In Python, string ascii_lowercase will give the lowercase letters ‘abcdefghijklmno...
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 Python, there are 2 ways to extract all the numbers contained in a string.
Using List Comprehension
List comprehensions provide a concise way to create lists.
If you only want to extract only positive integers, try the following:
#!/usr/bin/...