Skip to content

Instantly share code, notes, and snippets.

View campoy's full-sized avatar

Francesc Campoy campoy

View GitHub Profile
@campoy
campoy / letitfail.go
Last active January 6, 2024 07:26
This example shows how to have a set of goroutines running concurrently and processing requests. Panics from goroutines are recovered and the worker is restarted. You can download it and run it directly using `go run letitfail.go`
package main
import (
"bufio"
"fmt"
"os"
"time"
)
const numWorkers = 3
@campoy
campoy / qr.go
Created October 5, 2017 00:48
A simple program to print QR Codes on your terminal
// Copyright 2017 Google Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to writing, software distributed
// under the License is distributed on a "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied.
//
func merge(a, b <-chan int) <-chan int {
c := make(chan int)
go func() {
defer close(c)
for a != nil || b != nil {
select {
case v, ok := <-a:
if !ok {
fmt.Println("a is done")
a = nil
@campoy
campoy / sum.c
Created January 12, 2018 21:54
C function called from BigQuery
int sum(int a, int b)
{
return a + b;
}
@campoy
campoy / home.tmpl
Last active May 4, 2022 19:19
Electron-like html+golang app
<html>
<head>
<title>Hello, from Go</title>
</head>
<body>
<h1>Hello</h1>
<p>
This is Go code running, {{.}}
</p>
</body>
@campoy
campoy / Dockerfile
Created April 6, 2018 22:28
Kythe in a Docker image
FROM debian:jessie-backports as build
RUN apt-get update && apt-get install -y \
asciidoc asciidoctor source-highlight graphviz \
gcc libssl-dev uuid-dev libncurses-dev libcurl4-openssl-dev flex clang-3.5 bison \
parallel
RUN apt-get install -y -t jessie-backports openjdk-8-jdk
# install golang
RUN apt-get install -y curl
@campoy
campoy / main.go
Created February 12, 2018 17:31
gist 1 for episode 28 of justforfunc
func mergeRec(chans ...<-chan int) <-chan int {
switch len(chans) {
case 0:
c := make(chan int)
close(c)
return c
case 1:
return chans[0]
default:
m := len(chans) / 2
@campoy
campoy / main.go
Last active November 9, 2019 12:28
Dgraph: K-Shortest Path with all predicates
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"log"
"strings"
@campoy
campoy / main.go
Last active August 1, 2019 23:33
Estimating size for protobuf in memory
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"unsafe"
"github.com/creachadair/misctools/sizeof"
@campoy
campoy / query.js
Created July 30, 2019 05:19
Cameo query
{
spielberg as var(func: allofterms(name@en, "steven spielberg")) {
uid
}
cameos(func: uid(spielberg)) @cascade @normalize {
director : name@en
director.film {
film : name@en
starring {
performance.actor @filter(uid(spielberg)) {