In this post I will describe how defer works with a small sample code. The example Defer pretty much adds a command in a stack to be executed after a function call before it returns. The stack works in LIFO (last in first out) We will add two print to console commands in defer, just to see how the calls are handled. And then check it again in a loop. package golab import "fmt" //Defer1 func defer1() { defer fmt.Println("this is going to be printed after return") defer fmt.Println("this is a second command in defer") fmt.Println("this is A") fmt.Println("this is...
Posted on Oct 1, 2016
In this post I will describe how I did the setup for the google translate in this blog.
The setup
Go to Google Translate Site and click in the button to add to your website.
Input the URL of your site, in my case “mussatto.github.io”, with original language set to english.
Configure the plugin as desired. In my case I left the default options and then click on Show Code.
Then copy the code and paste it in a nice place in your code.
The Code
Check the code of this blog in here
Posted on Sep 30, 2016
In this post I will describe briefly how to setup Travis CI with GoLang on a github project. The setup Go to https://travis-ci.org/ and login with your github account. The example I will describe how I did the setup for Travis-CI for my golab github project Next to the “My Repositories”, click in the “+” button. After it has synced with github, your projects your appear in the list below. In my case, I clicked the button on the left of “mussatto/golab”. The Changes in Code Mainly, the thing to be done is add a file .travis.yml in the root...
Posted on Sep 30, 2016
In this post I will describe how to concatenate string in the right / more performatic way. The code will perform a long loop that appends some information in the current string The Setup package strings import "bytes" const maxIterations = 100000 func ConcatenateWrong() string { myVar := "" for i := 0; i < maxIterations; i++ { myVar = myVar + GenerateRandom() } return myVar } func ConcatenateRight() string { var buffer bytes.Buffer for i := 0; i < maxIterations; i++ { buffer.WriteString(GenerateRandom()) } return buffer.String() } The test package strings import ( "fmt" "testing" "time" ) func TestConcatenate(t...
Posted on Sep 29, 2016
In this post I will show briefly how to use type interface on function in Golang. The idea is to have a defined callback of function to be passed as parameter. In this example we will define a function that does some work with data, and then output the data in different ways in the console depending on the callback passed. The setup package abstraction type AfterWorkCallback func(data []int) string func Work(data []int, callback AfterWorkCallback){ //Do Work for index, value := range data{ data[index] = value + index } callback(data) } The test func Print(data []int) string { fmt.Println("data is:...
Posted on Sep 28, 2016
In this post I will describe briefly how Golang interfaces work. The interface The main idea we for this sample is: an animal, a Dog and a Cat that behaves as an animal. Here, we are defining which functions are required to a type to work as a Animal: package abstraction import "fmt" type Animal interface { Talk() string } The Dog In order to Dog to work as an Animal, the only thing you have to do is have the function with the same name (Talk), with the same parameters: type Dog struct { name string } func (dog...
Posted on Sep 26, 2016
In this post I will describe how I setup google analytics, google adsense and search in this jekyll blog. Google Analytics Google Analytics allows you to track who is acessing your website. Go to Google Analytics Website, login and add a new account. Its in Admin, accounts -> create new account. Then grab your tracking code, and put it somewhere in your page. In my case, I pasted it right before the </body> tag After a couple of minutes you should be able to see that you have been confirmed, and google analytics will grab all the statistics about your...
Posted on Sep 25, 2016
I am creating this post to describe how I setup the local environment for developing a blog in Jekyll + github pages. On github: Create a repository in the pattern: YOURUSERNAME.github.io (change YOURUSERNAME with your username in github) Install rbenv $ git clone https://github.com/rbenv/rbenv.git ~/.rbenv $ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc $ ~/.rbenv/bin/rbenv init $ source ~/.bashrc $ rbenv install 2.3.1 Then test your ruby installation: $ ruby --version ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux] Install the Jekyll gem: $ gem install jekyll Create a new site with Jekyll, and add git stuff: $ jekyll new YOURUSERNAME.github.io $ cd YOURUSERNAME.github.io...
Posted on Sep 24, 2016
In order to cover some basic features of golang, in this post I will provide a sample of how to write golang tests.
If we want to test a go file:
foo.go:
package main
func PrintSum(a, b int){
fmt.Printf("%v\n", a+b)
}
We need to create a new file, in the same package labeled:
foo_test.go
Then inside it, we create a function:
func TestPrintSum(t *testing.T){
PrintSum(1,2)
}
Then open a shell and type:
$ go test
And you should be able to see the test output.
Posted on Sep 22, 2016
In order to install GoLang in Ubuntu, go to this url, download the tar for Linux:
wget https://storage.googleapis.com/golang/go1.7.1.linux-amd64.tar.gz
Install go executables in the folder ~/dev/go1.7.1/
And the GoPath in ~/dev/gohome
mkdir -p ~/dev/go1.7.1/
mkdir -p ~/dev/gopath/
tar -xvf ~/dev/go1.7.1/
And then set the variables:
echo 'export GOROOT=$HOME/dev/go-1.7.1/go' >> ~/.bashrc
echo 'export GOPATH=$HOME/dev/gopath' >> ~/.bashrc
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
source ~/.bashrc
Then test it in a terminal:
$ go version
go version go1.7.1 linux/amd64