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 {
@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
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
@grahamking
grahamking / memory_example.rs
Last active Dec 16, 2015
Rust: Owner memory example
View memory_example.rs
fn main() {
let name: ~str,
other: ~str;
name = ~"Bob";
other = name;
println(other);
@grahamking
grahamking / use_sqlite.rs
Last active Dec 16, 2015
Rust: Use sqlite
View use_sqlite.rs
extern mod sqlite;
fn db() {
let database =
match sqlite::open("test.db") {
Ok(db) => db,
Err(e) => {
println(fmt!("Error opening test.db: %?", e));
return;
@grahamking
grahamking / net_fetch.rs
Created Apr 26, 2013
Rust: Connect to a socket and talk
View net_fetch.rs
extern mod std;
use std::{net_tcp,net_ip};
use std::uv;
fn fetch(code: ~str) -> ~[~str] {
let ipaddr = net_ip::v4::parse_addr("205.156.51.232");
let iotask = uv::global_loop::get();
let connect_result = net_tcp::connect(ipaddr, 80, &iotask);