Skip to content

Instantly share code, notes, and snippets.

Christophe Meessen chmike

Block or report user

Report or block chmike

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@chmike
chmike / httpGet.go
Last active Oct 10, 2019
Correct HTTP GET response handling
View httpGet.go
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 Oct 2, 2019
Relative Distinguish Name to string
View rdns2str.go
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 / sliceDump.go
Last active Mar 14, 2019
Go language function to dump a byte slice in hexadecimal and ASCII
View sliceDump.go
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(" ")
}
@chmike
chmike / GoGenerics-1.md
Created Sep 12, 2018
GoGenerics proposal
View GoGenerics-1.md

Go Generics proposal

After more than twenty five years of C++ programming, I'm still under its influence. It may be visible in this proposal. Also, english is not my native language. When in disagreement, make sure it isn't due to inaccurate expression on my part.

First, the concepts I will use in this document to avoid misunderstandings.

definition of generic type or function : type or function with generic parameters. generic parameters : the parameters of a generic definition. type of a generic parameter : the draft proposal calls them contracts. specialized generic definition : a generic definition with its generic arguments (types).

@chmike
chmike / checkDomain.go
Created Jun 29, 2018
Check domain name validity in Go
View checkDomain.go
// 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:
return fmt.Errorf("cookie domain: name length is %d, can't exceed 255", len(name))
}
View GoDeclareProblem.md

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 / GetTimeAsDouble.h
Created Dec 12, 2012
Get the current time as a double float in second units. Handy to compute processing time interval.
View GetTimeAsDouble.h
#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;
}
@chmike
chmike / IncrementalStats.h
Created Dec 12, 2012
Compute incrementally mean and std dev
View IncrementalStats.h
#include <cmath>
//! One kibibyte unit size (see http://en.wikipedia.org/wiki/Kibibyte)
const size_t KiB = 1024;
//! One mebibyte unit size (see http://en.wikipedia.org/wiki/Mebibyte)
const size_t MiB = 1024*1024;
//! Convert timeval structure to a double
double toDbl( struct timeval& t )
@chmike
chmike / PrintHex.h
Created Dec 12, 2012
Print binary data in haxadecimal
View PrintHex.h
#ifndef PRINTHEX_H
#define PRINTHEX_H
#include <stdio.h>
// Display data in hexadecimal
inline void printHex( const void* data, size_t len, const char* margin )
{
if( len == 0 )
{
You can’t perform that action at this time.