Skip to content

Instantly share code, notes, and snippets.

Avatar

Graham King grahamking

View GitHub Profile
@grahamking
grahamking / errs.go
Created May 22, 2020
Collect and handle multiple errors in Go
View errs.go
package util
import (
"errors"
"strings"
)
// Errs is an error that collects other errors, for when you want to do
// several things and then report all of them.
type Errs struct {
View alloc.s
.text
.global _start
_start:
mov $12, %rax # brk syscall number
mov $0, %rdi # 0 is invalid, want to get current position
syscall
mov %rax, %rsi # rsi now points to start of heap mem we'll allocate
@grahamking
grahamking / slicemap_test.go
Created Jun 30, 2015
Benchmark comparing map access vs slice search
View slicemap_test.go
package main
import (
"math/rand"
"testing"
"time"
)
const (
numItems = 100 // change this to see how number of items affects speed
@grahamking
grahamking / recv.go
Last active Aug 29, 2015
ICMP raw receive
View recv.go
package main
import (
"fmt"
"os"
"syscall"
)
func main() {
fd, _ := syscall.Socket(syscall.AF_INET, syscall.SOCK_RAW, syscall.IPPROTO_ICMP)
@grahamking
grahamking / send.go
Last active Aug 29, 2015
ICMP raw send
View send.go
package main
import (
"log"
"net"
"syscall"
)
func main() {
var err error
View online_upgrade.go
package main
import (
"bytes"
"encoding/gob"
"fmt"
"net"
"os"
"os/exec"
"os/signal"
@grahamking
grahamking / m1.rs
Created Apr 29, 2013
Using a module
View m1.rs
mod m2;
fn main() {
m2::say_hi();
}
@grahamking
grahamking / m2.rs
Created Apr 29, 2013
A module to use from a different file
View m2.rs
pub fn say_hi() {
println("Hi!");
}
@grahamking
grahamking / objects.rs
Last active Dec 16, 2015
Rust: Objects
View objects.rs
extern mod std;
use std::time;
struct User {
name: ~str,
age: int
}
impl User {
@grahamking
grahamking / mutable_pointers.rs
Last active Dec 16, 2015
Rust: The pointer and the box it points to
View mutable_pointers.rs
struct Point { x: int, y:int }
fn main() {
// Local variable, easy
let mut p1 = Point{x:1, y:2};
p1.x = 10;
// Owned pointer
// also easy because target inherts mutability
You can’t perform that action at this time.