Skip to content

Instantly share code, notes, and snippets.

View johnwesonga's full-sized avatar

johnwesonga

  • Bay Area
View GitHub Profile
@johnwesonga
johnwesonga / gist:5141298
Last active December 14, 2015 20:08
Python vs Go on string manipulation
In Python:
key = /cns/pb-d/home/engedu/text/shakespeare/hamlet:0000000000014fb9'
s = key.split("/")[-1]
parts = s.split(":")
print "%s:%s" % (parts[0], str(int(parts[1], 16)))
In Go:
import("fmt"
"strings"
"strconv"
@johnwesonga
johnwesonga / Read file in Go option #1
Created August 22, 2013 00:20
Reading a file in Go using the os package
package main
import (
"fmt"
"log"
"os"
)
func main() {
f, err := os.Open("sample.txt")
@johnwesonga
johnwesonga / Go read file - option #2
Created August 22, 2013 00:23
Read file in Go using ioutil
package main
import (
"fmt"
"io/ioutil"
"strings"
)
func main() {
f, err := ioutil.ReadFile("sample.txt")
@johnwesonga
johnwesonga / config
Created August 22, 2013 00:25
read yaml file in Go
username: admin
password: pass
@johnwesonga
johnwesonga / unique elements in a slice
Created August 22, 2013 00:29
Go code that ensures elements in a slice are unique
package main
import "fmt"
func uniqueNonEmptyElementsOf(s []string) []string {
unique := make(map[string]bool, len(s))
us := make([]string, len(unique))
for _, elem := range s {
if len(elem) != 0 {
if !unique[elem] {
@johnwesonga
johnwesonga / checker.go
Last active August 29, 2015 13:56
URL Checker
package main
import (
"bufio"
"fmt"
"io"
"log"
"net/http"
"os"
"sync"
@johnwesonga
johnwesonga / csvread.go
Last active August 29, 2015 13:56
Read a CSV file, skipping the headers
package main
import (
"encoding/csv"
"fmt"
_ "io"
"os"
)
func checkCsv() {
@johnwesonga
johnwesonga / add_profile.js
Last active August 29, 2015 13:57
Very Simple FlightJS app
// components/ui/add_profile.js
define(function (require) {
'use strict';
/**
* Module dependencies
*/
var defineComponent = require('flight/lib/component');
@johnwesonga
johnwesonga / remove-dup.go
Created May 27, 2014 11:58
Remove duplicate values from a slice
package main
import "fmt"
//O(n^2) time
func RemoveDuplicates(data []string) []string {
length := len(data) - 1
for i := 0; i < length; i++ {
for j := i + 1; j <= length; j++ {
if data[i] == data[j] {
@johnwesonga
johnwesonga / copyData
Created June 16, 2014 13:09
Apps Script to copy data from one column to multiple columns
function copyData() {
var mySheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var range = mySheet.getDataRange();
var firstNameCell = mySheet.getRange('A1');
var lastNameCell = mySheet.getRange('B1');
var row = 1;
//skip the header column
var dataRange = range.offset(1, 0, range.getNumRows()-1).getValues();
for (i = 0; i < dataRange.length; i++){