Skip to content

Instantly share code, notes, and snippets.

View chmike's full-sized avatar

Christophe Meessen chmike

View GitHub Profile
@chmike
chmike / sliceDump.go
Last active April 13, 2024 19:19
Go language function to dump a byte slice in hexadecimal and ASCII
func dumpByteSlice(b []byte) {
var a [16]byte
n := (len(b) + 15) &^ 15
for i := 0; i < n; i++ {
if i%16 == 0 {
fmt.Printf("%4d", i)
}
if i%8 == 0 {
fmt.Print(" ")
}
// NewRateLimiter returns a rate limiter function returning false
// when the event must be rejected, and true when it must be accepted.
// The rate is expressed in Hz (events/seconds). It simply ensures that
// the time interval between two accept satisfies the rate criteria. It
// is equivalent to the token bucket algorithm with a bucket size of one.
func NewRateLimiter(rate float64) func() bool {
var stamp time.Time // time stamp of last accepted event
return func() bool {
now := time.Now()
if now.Sub(stamp).Seconds() *rate < 1. {
@chmike
chmike / checkDomain.go
Last active December 1, 2023 19:21
Check domain name validity in Go
// Please use the package https://github.com/chmike/domain as is it maintained up to date with tests.
// checkDomain returns an error if the domain name is not valid.
// See https://tools.ietf.org/html/rfc1034#section-3.5 and
// https://tools.ietf.org/html/rfc1123#section-2.
func checkDomain(name string) error {
switch {
case len(name) == 0:
return nil // an empty domain name will result in a cookie without a domain restriction
case len(name) > 255:
package main
import (
"image/color"
"log"
"os"
"strings"
"gioui.org/app"
"gioui.org/f32"
package main
import (
"fmt"
"image"
"image/color"
"os"
"gioui.org/app"
"gioui.org/font/gofont"

Problems with Go's shorthand declaration :=

The shadow variable trap

The problem

It is best illustrated with the following Go code snippet.

var x int
@chmike
chmike / getlinefd.c
Last active February 26, 2020 15:56
getline using fd
// getlinefd reads a full line from fd into *lineptr. Initialize *lineptr to NULL and *n to 0.
// getlinefd will store the line into *lineptr which is is a buffer that will grow as needed.
// The buffer size is stored in *n. The line ends '\0' and with a '\n'when present.
// getlinefd returns the number of characters read, or -1 in case of error or if the end of file is reached.
ssize_t getlinefd(char **lineptr, size_t *n, int fd) {
static char buf[10];
static size_t len = 0;
const size_t blen = sizeof(buf);
if (lineptr == NULL || n == NULL) {
@chmike
chmike / httpGet.go
Last active November 8, 2019 10:00
Correct HTTP GET response handling
res, err := http.GET(...)
if res != nil {
defer func() {
_, err = io.Copy(ioutil.Discard, res.Body)
res.Body.Close()
}()
}
if err != nil {
// . . .
}
@chmike
chmike / rdns2str.go
Last active October 2, 2019 08:18
Relative Distinguish Name to string
var (
cnNameOid = asn1.ObjectIdentifier{2, 5, 4, 3}
emailOid = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 9, 1}
userIDOid = asn1.ObjectIdentifier{0, 9, 2342, 19200300, 100, 1, 1}
dcNameOid = asn1.ObjectIdentifier{0, 9, 2342, 19200300, 100, 1, 25}
)
// RDNSToString returns the Relative Distinguish Name as a string.
func RDNSToString(rdns *pkix.RDNSequence) string {
var buf strings.Builder
@chmike
chmike / GetTimeAsDouble.h
Created December 12, 2012 09:27
Get the current time as a double float in second units. Handy to compute processing time interval.
#include <sys/time.h>
// Return the current time as a double float in second units
double getTimeAsDouble()
{
struct timeval t;
::gettimeofday( &t, NULL );
return double(t.tv_sec) + double(t.tv_usec)/1000000.0;
}