Skip to content

Instantly share code, notes, and snippets.

func GetKeyURL(x int) (*url.URL, error) {
y := Foo(x)?
z := Bar(y)?
return Baz(z)
}
func Foo(x int) (int, error) {
...
}
type MyLongTypeName struct{
...
}
func foo(x int, y string) (MyLongTypeName, error) {
if x == 42 {
return MyLongTypeName{}, errors.New("x cannot be the meaning of life")
}
if y == "" {
type PrimaryColour int
const (
Red = iota
Yellow
Blue
)
func (c PrimaryColour) String() string {
switch c {
@ryanc414
ryanc414 / run_error.go
Created June 6, 2021 20:34
Run pattern with error handling in Go
func run(ctx context.Context) error {
var h Handler
ctx, cancel := context.WithCancel(ctx)
eg, ctx := errgroup.WithContext(ctx)
eg.Go(func() error { return h.Run(ctx) })
eg.Go(func() error {
for i := 0; i < 10; i++ {
select {
@ryanc414
ryanc414 / run.go
Created June 6, 2021 20:32
Run pattern in Go
func run(ctx context.Context) error {
var h Handler
ctx, cancel := context.WithCancel(ctx)
var wg sync.WaitGroup
wg.Add(1)
go func() {
h.Run(ctx)
wg.Done()
@ryanc414
ryanc414 / start_close.go
Created June 6, 2021 20:31
Start-Closer pattern in Go
func run(ctx context.Context) error {
var h Handler
h.Start(ctx)
defer h.Close()
for i := 0; i < 10; i++ {
select {
case <-time.After(time.Second):
log.Print(h.GetVal())
case <-ctx.Done():
@ryanc414
ryanc414 / lists.rs
Created September 24, 2020 22:36
linked lists in Rust
use std::collections::LinkedList;
fn main() {
let mut list: LinkedList<u32> = LinkedList::new();
list.push_back(2);
list.push_back(3);
list.push_back(5);
list.push_back(7);
list.push_back(11);
@ryanc414
ryanc414 / lists.go
Created September 24, 2020 22:35
linked lists in Go
package main
import (
"fmt"
"container/list"
)
func main() {
l := list.New()
l.PushBack(2)
@ryanc414
ryanc414 / process_json.go
Created September 24, 2020 19:33
Process a JSON file, in Go
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
"os"
)
class AppClient:
def __init__(self, app_addr):
self._app_addr = app_addr
def request(self, method, path):
return requests.request(method, f"{self._app_addr}{path}")
@pytest.fixture(scope="function")
def app_client(app):