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...
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 ...
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 Python, there are 2 do case insensitive string comparison.
Using casefold Method
The str.casefold() method returns a casefolded copy of the string. Casefolded strings may be used for caseless matching. For example,
#!/usr/bin/python3
a = "H...
In Python, there are 2 ways to convert all strings in a list to int.
Using map Method
You can convert all strings in a list to int using the map method. For example,
#!/usr/bin/python3
a = ["1", "2", "4", "5"]
b = list(map(int, a))
print(b)
...
In Golang, there are 3 ways to remove invalid UTF-8 characters from a string.
Using ToValidUTF8 Function
The strings.ToValidUTF8() function returns a copy of the string s with each run of invalid UTF-8 byte sequences replaced by the replacement str...
In Golang, there are 2 ways to check if a file is a valid image.
Using DetectContentType Function
The http.DetectContentType() function implements the algorithm described at https://mimesniff.spec.whatwg.org/ to determine the Content-Type of the gi...
In Golang, there are 2 ways to count characters in a string.
Using RuneCountInString Function
Straight forward natively use the utf8.RuneCountInString()
The following example should cover whatever you are trying to do:
package main
import (
...
In Python, using the os.environ variable is the easiest way to set environment variables
Using os.environ Variable
os.environ behaves like a python dictionary, so all the common dictionary operations can be performed. In addition to the get and set ...
In Python, there are 3 ways to remove all empty strings from a list of strings.
Using filter Function
The filter(function, iterable) function constructs an iterator from those elements of iterable for which function returns true.
The following exam...
In Python, using the str.join function is the easiest way to concatenate a list of strings into a single string
Using str.join Function
The str.join() method returns a string which is the concatenation of the strings in iterable. A TypeError will be...
In Golang, using the strings.EqualFold function is the easiest way to check if two strings are equal in a case-insensitive manner
Using strings.EqualFold Function
The strings.EqualFold() function reports whether s and t, interpreted as UTF-8 strings...
In Python, using the triple-quoted strings is the easiest way to create multiline comments.
Using triple-quoted Strings
String literals can span multiple lines. One way is using triple-quotes: """...""" or '''...'''.
You can use triple-quoted strin...