The Go Cookbook

Maintained by SuperOrbital.

A community built and contributed collection of practical recipes for real world Golang development.

View project on GitHub

The Go Cookbook is built with the help of people like yourself. Please support this site and help others by contributing a recipe of your own.


Running & Packaging

Strings

Numbers

  • [work in progress] Representing Enums in Go
  • [work in progress] Checking Whether a String Is a Valid Number
  • Rounding Floating-Point Numbers
  • Comparing Floating-Point Numbers
  • Operating on a Series of Integers
  • Working with Roman Numerals
  • Generating Random Numbers
  • Generating Repeatable Random Number Sequences
  • Making Numbers Even More Random
  • Generating Biased Random Numbers
  • Doing Trigonometry in Degrees, Not Radians
  • Calculating More Trigonometric Functions
  • Taking Logarithms
  • Multiplying Matrices
  • Using Complex Numbers
  • Converting Binary, Octal, and Hexadecimal Numbers
  • Putting Commas in Numbers
  • Printing Correct Plurals

Dates and Times

  • Finding Today's Date
  • Converting DMYHMS to Epoch Seconds
  • Converting Epoch Seconds to DMYHMS
  • Adding to or Subtracting from a Date
  • Difference of Two Dates
  • Day in a Week/Month/Year or Week Number
  • Parsing Dates and Times from Strings
  • Printing a Date
  • High-Resolution Timers
  • Short Sleeps

Arrays

  • Specifying a List in Your Program
  • Printing a List with Commas
  • Changing Array Size
  • Implementing a Sparse Array
  • Iterating Over an Array
  • Iterating Over an Array by Reference
  • Extracting Unique Elements from a List
  • Finding Elements in One Array but Not Another
  • Computing Union, Intersection, or Difference of Unique Lists
  • Appending One Array to Another
  • Reversing an Array
  • Processing Multiple Elements of an Array
  • Finding the First List Element That Passes a Test
  • Finding All Elements in an Array Matching Certain Criteria
  • Sorting an Array Numerically
  • Sorting a List by Computable Field
  • Implementing a Circular List
  • Randomizing an Array

Maps

  • Adding an Element to a Map
  • [work in progress] Testing for the Presence of a Key in a Map
  • Deleting from a Map
  • Traversing a Map
  • Convert a Map into an Array of keys or values
  • Printing a Map
  • Mapes with Multiple Values per Key
  • Inverting a Map
  • Sorting a Map
  • Merging Maps
  • Finding Common or Different Keys in Two Maps

Files

  • Checking the Existence of a File
  • Reading and Writing to Files
  • Expanding Tildes in Filenames
  • Creating Temporary Files
  • Storing a File Inside Your Program Text
  • Storing Multiple Files in the DATA Area
  • Writing a Unix-Style Filter Program
  • Modifying a File in Place with a Temporary File
  • Modifying a File in Place with the -i Switch
  • Modifying a File in Place Without a Temporary File
  • Locking a File
  • Flushing Output
  • Doing Non-Blocking I/O
  • Determining the Number of Unread Bytes
  • Reading from Many Filehandles Without Blocking
  • Reading an Entire Line Without Blocking
  • Counting Lines (or Paragraphs or Records) in a File
  • Processing Every Word in a File
  • Reading a File Backward by Line or Paragraph
  • Trailing a Growing File
  • Picking a Random Line from a File
  • Randomizing All Lines
  • Reading a Particular Line in a File
  • Processing Variable-Length Text Fields
  • Removing the Last Line of a File
  • Processing Binary Files
  • Using Random-Access I/O
  • Updating a Random-Access File
  • Reading a String from a Binary File
  • Reading Fixed-Length Records
  • Reading Configuration Files
  • Testing a File for Trustworthiness
  • Reading or Writing Unicode from a Filehandle
  • Comparing the Contents of Two Files
  • Reading and writing JSON files
  • Reading and writing YAML files

Directories

  • Getting and Setting Timestamps
  • Deleting a File
  • Copying or Moving a File
  • Recognizing Two Names for the Same File
  • Processing All Files in a Directory
  • Globbing, or Getting a List of Filenames Matching a Pattern
  • Processing All Files in a Directory Recursively
  • Removing a Directory and Its Contents
  • Renaming Files
  • Splitting a Filename into Its Component Parts
  • Working with Symbolic File Permissions Instead of Octal Values

Goroutines

Systems Programming

  • Catching signals
  • Running Child Processes
  • Processing Command-line Arguments

Sockets

  • Writing a TCP Client
  • Writing a TCP Server
  • Communicating over TCP
  • Setting Up a UDP Client
  • Setting Up a UDP Server
  • Using Unix Domain Sockets
  • Identifying the Other End of a Socket
  • Finding Your Own Name and Address
  • Closing a Socket After Forking
  • Writing Bidirectional Clients
  • Forking Servers
  • Pre-Forking Servers
  • Non-Forking Servers
  • Multitasking Server with Threads
  • Writing a Multitasking Server with POE
  • Writing a Multihomed Server
  • Making a Daemon Server
  • Restarting a Server on Demand
  • Managing Multiple Streams of Input

Internet Services

  • Simple DNS Lookups
  • Sending Mail
  • Reading and Posting Usenet News Messages
  • Reading Mail with POP3
  • Simulating Telnet from a Program
  • Pinging a Machine
  • Accessing an LDAP Server
  • Sending Attachments in Mail
  • Extracting Attachments from Mail
  • Writing a RESTful service
  • Connecting to MySQL
  • Connecting to PgSQL
  • Fetching a URL
  • Setting HTTP Headers
  • Automating Form Submission
  • Extracting URLs
  • Converting ASCII to HTML
  • Converting HTML to ASCII
  • Extracting or Removing HTML Tags
  • Finding Stale Links
  • Finding Fresh Links
  • Using Templates to Generate HTML
  • Mirroring Web Pages
  • Creating a Robot
  • Parsing a Web Server Log File
  • Processing Server Logs
  • Using Cookies
  • Fetching Password-Protected Pages
  • Fetching https:// Web Pages
  • Resuming an HTTP GET
  • Parsing HTML
  • Extracting Table Data
  • Processing POST requests