Skip to content

A mostly complete guide to Go's copy function

I came across a use of the copy function that made my eyes glaze over. Initially I had no idea what I was looking at. Here’s the relevant section of code. The goal is to add numbers to a sorted set. sorted := []int{} for i := x; i < len(nums); i++ { n := nums[i-x] idx, found := slices.BinarySearch(sorted, n) if !found { sorted = append(sorted, 0) /* ========= LOOK HERE ========= */ copy(sorted[idx+1:], sorted[idx:]) sorted[idx] = n } // ... } I wanted to understand what was happening so I started from the beginning and developed this guide for myself. Hopefully you will find it useful as well.

Read more →

JavaScript Iterators

Iterators offer an alternative to the classic for loop in JavaScript/TypeScript. You have probably used them without even knowing it. const arr = [1, 2, 3]; // `for...of` loops use iterators for (const el of arr) { // do something } The for...of loop will feature prominently throughout this article but there are several other common use case for iterators such as spread syntax.

Read more →