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

How to Fetch Return Value from a goroutine in Golang

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

How to Use Deferred Function Calls in Golang

Understanding defer in Go. Deferred Function Calls A defer statement is an ordinary function or method call prefixed by the keyword defer. The function and argument expressions are evaluated when the statement is executed, but the actual call is def...
ada

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 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 Get Name of Current Script in Python

In Python, using the __file__ is the easiest way to get name of current script. Using Module Attributes You can use __file__ to get the name of the current file. When used in the main module, this is the name of the script that was originally invoke...
Unused

How to Get a SHA256 Hash from a String in Go

In Golang, using the Sum256 function is the easiest way to get a SHA256 hash from a string.Go implements several hash functions in various crypto/* packages. Using Sum256 Function The sha256.Sum256() function returns the SHA256 checksum of the data...
ada

How to Get a MD5 Hash from a String in Go

In Golang, using the Sum function is the easiest way to get a MD5 hash from a string. Using Sum Function The md5.Sum() function returns the MD5 checksum of the data. The following example should cover whatever you are trying to do: package mai...
aweis

How to Delete the Contents of a Folder in Python

In Python, there are 2 ways to delete the contents of a folder. Using rmtree Method The shutil.rmtree() method deletes an entire directory tree; path must point to a directory. For example, #!/usr/bin/python3 # Import module import os, shutil ...
Tomoki

How to Check if Directory on Path is Empty in Go

In Golang, using the Readdirnames function is the easiest way to check if directory on path is empty. Using Readdirnames Function The file.Readdirnames(n int) function reads the contents of the directory associated with file and returns a slice of ...
Sambhav Khandelwal

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 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 Get a List of All Subdirectories in the Current Directory in Python

In Python, there are 2 ways to get a list of all subdirectories in the current directory. Using os.walk Function The os.walk() method generates the file names in a directory tree by walking the tree either top-down or bottom-up. The following examp...
pooriabt