Instantly share code, notes, and snippets.

View Go Guru.md

Go Guru

See official doc: Using Go Guru

go get golang.org/x/tools/cmd/guru
guru -help

# guru <mode> <position:byte offset>
# 
View Vim Regex.md

A zero-width negative lookaround assertion looks like:

level='(?!info)

Note: this would match level='error' or level='debug'.

The equivalent in Vim is:

View Bash loop files in a directory and check their types.sh
#!/bin/bash
project=~/foo
service=bar
ignore=("cmd" "lib" "scripts")
for filename in $project/$service/*; do
if [[ -d $filename ]]; then
echo "$filename is a directory"
View Long and Short Flag.go
var myFlagType string
func init() {
const (
flagValue = "default value is foo"
flagUsage = "this is my flag explanation"
)
flag.StringVar(&myFlagType, "foo", flagValue, flagUsage)
flag.StringVar(&myFlagType, "f", flagValue, flagUsage+" (shorthand)")
}
View 1. Python Dynamic Log Levels via Abstraction.py
import logging
def log_it(msg, level='error'):
fn = getattr(logging, level)
fn(msg)
log_it(1, level='warning')
# WARNING:root:1
log_it(1, level='error')
View GitHub Collapsible Drop Down Menus.md

collapsible markdown?

CLICK ME

yes, even hidden code blocks!

print("hello world!")
View Python Tornado Generic Exception Handling.py
"""
Exception is still sent to stdout, so if your service is configured
to send all stdout to a log aggregator you'll get both a load of
log noise followed by a single log call that consolidates it all.
See https://gist.github.com/1140bcd773616ecdae9bb4d2e9e55b34 for a
logging/metrics abstraction (+ tornado implementation) that ties
this write_error function together nicely.
"""
View Python Decorator Order.py
"""
Decorators are 'bottom up', meaning:
The bottom decorator is executed, and its result is
provided as input to the decorator above it.
In the below code this would look something like:
makebold(makeitalic(hello()))
"""
View Modern JS.md

Ensure you have Node/NPM installed, then make a project directory:

mkdir modern-js && cd modern-js

Create an empty package.json file:

npm init -y
View Fastly Edge Dictionaries API Examples.bash
#!/usr/bin/env bash
# API documentation here...
# https://docs.fastly.com/api/config
#
# at the end of the day, these are just simple `curl` requests
# along with a HTTP header that holds our api token.
#
# meaning, you could rewrite all of this much more cleanly in
# a more fully feature language such as Go or Python.