Skip to content

Instantly share code, notes, and snippets.

View pascaldekloe's full-sized avatar

Pascal S. de Kloe pascaldekloe

View GitHub Profile
@pascaldekloe
pascaldekloe / utf8.js
Last active September 9, 2023 05:21
JavaScript UTF-8 encoding and decoding with TypedArray
// This is free and unencumbered software released into the public domain.
// Marshals a string to an Uint8Array.
function encodeUTF8(s) {
var i = 0, bytes = new Uint8Array(s.length * 4);
for (var ci = 0; ci != s.length; ci++) {
var c = s.charCodeAt(ci);
if (c < 128) {
bytes[i++] = c;
continue;
@pascaldekloe
pascaldekloe / stream.go
Created January 4, 2017 19:20
Streaming Colfer records example
func writeAll(w io.Writer, records []*batch.Record) error {
var buf []byte
for _, r := range records {
n, err := r.MarshalLen()
if err != nil {
return err
}
if n > len(buf) {
buf = make([]byte, n*2)
@pascaldekloe
pascaldekloe / Colfer.c
Last active April 2, 2017 22:19
Demo C Compilation
// Code generated by colf(1); DO NOT EDIT.
// The compiler used schema file demo.colf for package demo.
#include "Colfer.h"
#include <errno.h>
#include <stdlib.h>
#if defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN || \
defined(__BIG_ENDIAN__) || \
@pascaldekloe
pascaldekloe / Colfer.go
Created April 2, 2017 22:20
Demo Go Compilation
// Package demo offers a demonstration.
// These comment lines will end up in the generated code.
package demo
// Code generated by colf(1); DO NOT EDIT.
// The compiler used schema file demo.colf.
import (
"encoding/binary"
"fmt"
@pascaldekloe
pascaldekloe / Colfer.js
Last active April 2, 2017 22:25
Demo JavaScript Compilation
// Code generated by colf(1); DO NOT EDIT.
// The compiler used schema file demo.colf for package demo.
// Package demo offers a demonstration.
// These comment lines will end up in the generated code.
var demo = new function() {
const EOF = 'colfer: EOF';
// The upper limit for serial byte sizes.
var colferSizeMax = 16 * 1024 * 1024;
@pascaldekloe
pascaldekloe / Course.java
Last active April 7, 2017 01:19
Demo Java Compilation
package com.example.demo;
// Code generated by colf(1); DO NOT EDIT.
import static java.lang.String.format;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
@pascaldekloe
pascaldekloe / dmesg
Last active May 1, 2018 11:50
OpenBSD 6.3 - Panasonic ToughBook CF-31 mk2, 3G, GPS
OpenBSD 6.3 (GENERIC.MP) #107: Sat Mar 24 14:21:59 MDT 2018
deraadt@amd64.openbsd.org:/usr/src/sys/arch/amd64/compile/GENERIC.MP
real mem = 8456351744 (8064MB)
avail mem = 8192995328 (7813MB)
mpath0 at root
scsibus0 at mpath0: 256 targets
mainbus0 at root
bios0 at mainbus0: SMBIOS rev. 2.6 @ 0xeb360 (31 entries)
bios0: vendor American Megatrends Inc. version "V2.00L12" date 08/01/2011
bios0: Panasonic Corporation CF-31JECAJFG
@pascaldekloe
pascaldekloe / int64.js
Created April 22, 2017 11:02
Big-endian 64-bit integer decoding with TypedArray
// Unmarshals an Uint8Array as a signed 64-bit integer, big-endian.
function decodeInt64(data, i, allowImprecise) {
var v = 0, j = i + 7, m = 1;
if (data[i] & 128) {
// two's complement
for (var carry = 1; j >= i; --j, m *= 256) {
var b = (data[j] ^ 255) + carry;
carry = b >> 8;
v += (b & 255) * m;
}
@pascaldekloe
pascaldekloe / unit.go
Last active July 25, 2020 18:52
Concept: I/O units with Go
package io
// ReadUniter provides an extension pattern for transaction metadata.
type ReadUniter interface {
Reader
ReadUnits() Units // best effort: possibly zero
}
// WriteUniter provides an extension pattern for transaction metadata.
type WriteUniter interface {

Go Project Structure Standards

Now that Go undergoes mainstream adoption, you can witness a hunger for more and more convention. The core guidelines are impressive, but obviously they can not cover everything. The following sections address some ongoing attempts on the matter.

Reusable Code

All code is pkg if the definition is "library code that's OK to use by external applications”, with the exception for main packages and any code placed in an internal (sub)directory. Using pkg is the same redundancy as using src in a source code repository/directory, or a java directory with .java files.

Yes, there are many projects that don't write APIs in a reusable manner, just like many projects don't follow the compatibility rules from the semantic versioning model. None of it means that we need extra nesting to get it right.