November 27, 2014
#!/bin/bash

# Instalar o actualizar mono y monodevelop.
# La primera vez llamara init para instalar todas las dependencias.

DOWNLOAD_DIR="/syltek/downloads"
INSTALL_DIR="/syltek/bin"

init() {
    mozroots --import --sync
    sudo apt-get install -y make libx11-dev libexif-dev libjpeg-dev libpng-dev autoconf \
                automake libtool gettext libglib2.0-dev libpng-dev  g++ gettext \
                libfontconfig1-dev mono-gmcs git build-essential automake checkinstall \
                intltool mono-complete mono-addins-utils libglib2.0-cil-dev gtk-sharp2 \
                gnome-sharp2 monodevelop-nunit monodevelop-versioncontrol
    mkdir -p $DOWNLOAD_DIR
    cd $DOWNLOAD_DIR
    git clone https://github.com/mono/monodevelop mdmaster
    cd $DOWNLOAD_DIR
    git clone https://github.com/mono/mono monomaster
}


updateMono() {
    cd $DOWNLOAD_DIR/monomaster
    git reset --hard
    git checkout master
    git pull
    ./autogen.sh --prefix=$INSTALL_DIR/monomaster
    make && make install
}

updateMonodevelop() {
    cd $DOWNLOAD_DIR/mdmaster
    git reset --hard
    git checkout master
    git pull 
    git pull --recurse-submodules
    git submodule update --recursive
    ./configure --prefix=$INSTALL_DIR && make && make install
}


usage () {
  echo "Usage: monoUpdate.sh [all, mono, monodevelop, init]"
  exit 1
}

if (( $# != 1 )); then
    usage
fi

if [ "$1" == "all" ]; then
    updateMono
    updateMonodevelop
elif [ "$1" == "mono" ]; then
    updateMono
elif [ "$1" == "monodevelop" ]; then
    updateMonodevelop
elif [ "$1" == "init" ]; then
    init
else
    usage
fi




November 9, 2014
test.c:
#include <string.h>

int indexOf(const char *a, const char *b) {
    char *i = strstr(a, b);
    if (i!=NULL)  {
        return i - a;
    }
    return -1;
}
test.h:
int indexOf(char *a, char *b);
test.go:
package main

import (
    "fmt"
    "strings"
)

// #cgo CFLAGS: -I.
// #cgo LDFLAGS: -L. -ltest
// #include <test.h>
import "C"

func main() {
    a := "Hello World"
    b := "Wo"
    fmt.Println("C:", indexOf(a, b, true))
    fmt.Println("GO:", indexOf(a, b, false))
}

func indexOf(a, b string, cgo bool) int {
    if cgo {
        i := C.indexOf(C.CString(a), C.CString(b))
        return int(i)
    }

    return strings.Index(a, b)
}
Compile test.c as a library:
$ gcc -Wall -g -c test.c -static -o test
$ ar ruv libtest.a test
build it statically linked:
$ go build --ldflags '-extldflags "-static"' test.go
try it:
$ ./test
C: 6
GO: 6
$ file test
test: ELF 64-bit LSB  executable, x86-64, version 1 (GNU/Linux), statically linked

Benchmarking

bench_test.go:
package main

import "testing"

var str1 string = "Hello World"
var str2 string = "Wo"

func BenchmarkCgo(b *testing.B) {
    for i := 0; i < b.N; i++ {
        indexOf(str1, str2, true)
    }
}

func BenchmarkGo(b *testing.B) {
    for i := 0; i < b.N; i++ {
        indexOf(str1, str2, false)
    }
}
$ go test -bench . -benchmem
testing: warning: no tests to run
PASS
BenchmarkCgo    10000000           278 ns/op           0 B/op           0 allocs/op
BenchmarkGo    50000000            38.4 ns/op           0 B/op           0 allocs/op
ok      foo/cgo2    5.080s
Embedding C code directly in the go source file:
package main

/*
    char* foo(void) {
        return "hello, world!";
    }
*/
import "C"

import "fmt"

func main() {
    fmt.Println(C.GoString(C.foo()))
}

November 9, 2014
Go programs are statically linked so you don't need any dependency.
package main

import (
    "fmt"
)

func main() {
    fmt.Println("Hello")
}
You can check it:
$ go build test.go
$ file test
test: ELF 64-bit LSB  executable, x86-64, version 1 (SYSV), statically linked, not stripped
But if your program uses the net package, cgo is enabled by default:
package main

import "net"

func main() {
    net.Dial("tcp", "")
}
If you compile this program it will be dynamically linked:
$ go build test.go
$ file test
test: ELF 64-bit LSB  executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), not stripped
$ ldd test
    linux-vdso.so.1 =>  (0x00007fffd37fe000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f599faaa000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f599f6e4000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f599fcec000)
You can disable cgo with CGO_ENABLED=0 but you have to rebuild the go-toolchain with the flag. This will still link dynamically:
$ export CGO_ENABLED=0
$ go build test.go 
$ file test
test: ELF 64-bit LSB  executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), not stripped
Same result. But there is a flag that can disable cgo just for the net package:
$ go build  -tags netgo -a test.go
$ file test
test: ELF 64-bit LSB  executable, x86-64, version 1 (SYSV), statically linked, not stripped

August 29, 2014

Update local feature branch to sync with master:

git rebase master

Squash last 4 commits:

git rebase -i HEAD~4
Opens an editor where you can enter commands (described below) for each commit to be rebased. These commands determine how individual commits will be transferred to the new base. You can also reorder the commit listing to change the order of the commits themselves.

Generate commits to cancel previous specific commit

git revert <sha>
Creates a new commit that cancels the changes made by the <sha> commit.

https://www.atlassian.com/git/tutorial/rewriting-git-history

August 28, 2014
#!/bin/bash
sendalert () {
    echo "[`date "+%y-%m-%d %H-%M"`] Replication ERROR $SERVER" >> replication.log
    ./sendmail.sh "$SERVER" "Replication error $SERVER"
}
testServer() {
    printf "$INSTANCE..."
    test=$(ssh -p$PORT root@$SERVER "mysql -e 'show slave status\G'")
    if [[ $test != *"Slave_IO_Running: Yes"* ]]; then
        printf " IO stopped \n"
        sendalert
        return
    fi
    if [[ $test != *"Slave_SQL_Running: Yes"* ]]; then
        printf " SQL slave stopped \n"
        sendalert
        return
    fi
    seconds=$(echo "$test" | grep "Seconds_Behind_Master" | cut -d ':' -f 2)
    printf " $seconds seconds behind master"
    if [[ $seconds -gt 20 ]]; then
        printf " $seconds seconds behind master \n"
        sendalert
        return
    fi
    printf " ...OK\n"
}
PORT=25
for INSTANCE in 2 3 4 5
do
    SERVER=host$INSTANCE.xxxx.com
    testServer
done