Skip to content

Instantly share code, notes, and snippets.

@Rich-Harris
Rich-Harris / README.md
Last active February 27, 2025 11:20
Testing array.splice vs array.pop vs set.delete

You have an array. Its sort order doesn't matter. You want to remove an item from this array.

The obvious thing to do would be to use splice:

function remove(array, item) {
  const index = array.indexOf(item);
  array.splice(index, 1);
}
@jerblack
jerblack / Elevate when needed in Go.md
Last active March 10, 2025 16:02
Relaunch Windows Golang program with UAC elevation when admin rights needed.

I'm buiding a command line tool in Go that has an option to install itself as a service on Windows, which it needs admin rights for. I wanted to be able to have it reliably detect if it was running as admin already and if not, relaunch itself as admin. When the user runs the tool with the specific switch to trigger this functionality (-install or -uninstall in my case) they are prompted by UAC (User Account Control) to run the program as admin, which allows the tool to relaunch itself with the necessary rights.

To detect if I was admin, I tried the method described here first:
https://coolaj86.com/articles/golang-and-windows-and-admins-oh-my/
This wasn't accurately detecting that I was elevated, and was reporting that I was not elevated even when running the tool in CMD prompt started with "Run as Administrator" so I needed a more reliable method.

I didn't want to try writing to an Admin protected area of the filesystem or registry because Windows has the ability to transparently virtualize those writes

@jerblack
jerblack / PendingFileRenameOperations.go
Created December 1, 2019 18:10
GoLang: Using PendingFileRenameOperations in the Windows Registry to delete/rename files on reboot with Go.
package main
import (
"fmt"
"golang.org/x/sys/windows"
"golang.org/x/sys/windows/registry"
"log"
"os"
"strings"
"syscall"
@davidair
davidair / MainForm.cs
Last active August 12, 2022 06:39
Sample for self-registering desktop C# app that creates and reacts to toasts
/**
* Copyright 2020 Google LLC.
* SPDX-License-Identifier: MIT
*/
using Microsoft.Toolkit.Uwp.Notifications;
using System;
using System.Windows.Forms;
using Windows.Data.Xml.Dom;
using Windows.UI.Notifications;
@echo off
SET NODE_ENV=production
SET /A DELAY = 0
TITLE (%DELAY%) %~nx0
:loop
node "app.js"
SET /A DELAY = %DELAY% + 10
TITLE (%DELAY:~0,-1%) %~nx0
@spddl
spddl / 1#interval.md
Last active January 5, 2021 12:30
I have X different intervals that should all run in parallel with the largest possible time interval between them.

I have X different intervals that should all run in parallel with the largest possible time interval between them.

Input:

1m, 1m

Output:

1m started now, next: 1m0s, 2m0s, 3m0s, 4m0s