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

How to Use Variadic Functions in Golang

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

How to Use bytes.Buffer in Golang

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

How to Format a Currency with Commas in Golang

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

How to Get correct file base name in Golang

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

How to Use Strings in Golang

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

How to Do Case Insensitive String Comparison in Python

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

How to Convert all Strings in a List to Int in Python

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

How to Remove Invalid UTF-8 Characters from a String in Go

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

How to Check if a File is a Valid Image in Go

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

How to Count Characters in a String in Go

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

How to Set Environment Variables in Python

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

How to Remove all Empty Strings from a List of Strings in Python

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

How to Concatenate a List of Strings into a Single String in Python

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

How to Check If two Strings are Equal in a Case-Insensitive Manner in Go

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

How to Create Multiline Comments in Python

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