Golang: Database migration to manage your database schema by creating incremental SQL

In this post I write down my experience regarding  database migration configuration for Golang using Goose database migration.

Goose installation

$ go get -u github.com/pressly/goose/cmd/goose

This will install the goose binary to your $GOPATH/bin directory.

1.Install
Goose binary installed in bin folder

How to use

$ goose

2.run_goose
Goose executable in action

Create a SQL migration using Goose

$ postgres=# CREATE DATABASE teahrm

3.create-users
Golang SQL migration using Goose
8.empty-migration-generated.png
Just generated empty Migration

Create target database to migrate with

$ goose create create_tables_users sql

4.create-database.png
List of existing databases

Connect to the PostgresSQL database thru psql

$ postgres=# \connect teahrm

Show tables

$ teahrm=# \dt

7.connect-database
Empty database without table yet

Create migration for table users (PostgreSQL datatype)

Migrate Up

goose postgres "user=postgres dbname=teahrm sslmode=disable" up

13.migrate-up.png

Migrate Down

goose postgres "user=postgres dbname=teahrm sslmode=disable" down

14.migrate-down

Common mistake while migrating up

10-common-mitsake
Missing connection information
12.wrongdbname

Wrong database name: goose: no migrations to run. current version: 1

Check Connection status to avoid connection error

goose postgres "user=postgres dbname=postgres sslmode=disable" status

11.connectionstatus

That’s all guys… have a nice weekend…

 

 

 

 

 

Advertisements

Golang: Web development using Gorilla Mux and Pongo2 templating engine

What is Golang Gorilla ?

Gorilla is a web toolkit for the Go programming language.

What is Gorilla mux?

The name mux stands for “HTTP request multiplexer”. Like the standard http.ServeMux, mux.Router matches incoming requests against a list of registered routes and calls a handler for the route that matches the URL or other conditions.

What is HTTP Multiplexer / Mux?

ListenAndServe starts an HTTP server with a given address and handler. The handler is usually nil, which means to use DefaultServeMux. Handle and HandleFunc add handlers to DefaultServeMux

What is Pongo2 ?

pongo2 is the successor of pongo, a Django-syntax like templating-language.

pongo is a well-tested template engine which implements a Django-template-like syntax.

Install Gorilla Mux and Pongo

go get github.com/gorilla/mux
go get -u github.com/flosch/pongo2

Create app.yml

  runtime: go
  api_version: go1 

  handlers:
  - url: /.*
    script: _go_app
    secure: always
    redirect_http_response_code: 301 

create main.go

 package main

  import (
      "net/http"

      pongo "github.com/flosch/pongo2"
      "github.com/gorilla/mux"
  )

  func init() {
      r := mux.NewRouter()

      r.HandleFunc("/", homeHandler)  

      http.Handle("/", r)
  }

  func homeHandler(w http.ResponseWriter, r *http.Request) {
      template := "dashboard.html"
      tmpl := pongo.Must(pongo.FromFile(template))

      err := tmpl.ExecuteWriter(pongo.Context{"title": "Index", "greating": "Hallo"}, w)
      if err != nil {
          http.Error(w, err.Error(), http.StatusInternalServerError)
      }
  }

create base.html

 <html>
      <head>
          <title>
              {% block title %}Website Title Default{% endblock %}
          </title>
      </head>
      <body>
          {% block body %}Body Default{% endblock %}
      </body>
  </html>

create dashboard.html

  {% extends "base.html" %}

  {% block title  %}Dashboard Real Title{% endblock %}

  {% block body %}Hallo world Gorilla!{% endblock %}

Running the code on the local server


dev_appserver.py --port=9999 ./

wakakakak1

bowser1

 

Note: I am using App Engine local environment development

Golang Web: Setting Up GIN Framework with Pongo2 template engine on GAE Standard env

What Is Gin

Gin is a web framework written in Golang. It features a martini-like API with much better performance, up to 40 times faster. If you need performance and good productivity, you will love Gin.

What Is Pongo

Pongo is a well-tested template engine which implements a Django template like syntax and pongo2 is the successor of pongo, a Django-syntax like templating-language.

For Pongo documentation on how the templating language works you can head over to the Django documentation. pongo2 aims to be compatible with it (link).

Ok then. let’s start the journey

Installing GIN

go get github.com/gin-gonic/gin

go get github.com/gin-gonic/gin

Installing Pongo2

go get -u github.com/flosch/pongo2 go get -u github.com/flosch/pongo2

Install robvdl/pongo2gin

Package pongo2gin is a template renderer that can be used with the Gin web framework gin-gonic/gin it uses the Pongo2 template library flosch/pongo2 to use pongo2gin you need to set your router.HTMLRenderer to a new renderer instance, this is done after creating the Gin router when the Gin application starts up. You can use pongo2gin.Default() to create a new renderer with default options, this assumes templates will be located in the “templates” directory, or you can use pongo2.New() to specify a custom location.To render templates from a route, call c.HTML just as you would with regular Gin templates, the only difference is that you pass template data as a pongo2.Context instead of gin.H type.

Create app.yml

  runtime: go
  api_version: go1 
  
  handlers:
  - url: /.* 
    script: _go_app
    secure: always
    redirect_http_response_code: 301 

Create main.go

 package main

  import (
      "net/http"

      "github.com/flosch/pongo2"
      "github.com/gin-gonic/gin"
      "github.com/robvdl/pongo2gin"
  )

  func init() {
      r := gin.Default()

      r.HTMLRender = pongo2gin.Default()

      r.LoadHTMLGlob("templates/*.html")

      r.GET("/", func(c *gin.Context) {
          c.HTML(http.StatusOK, "main.html", pongo2.Context{
              "title":    "Hello world !",
              "greating": "Hi, Hello world my mate!",
          })
      })

      http.Handle("/", r)
  }

Create templates/main.html

  <html>
      <head>
          <title>
              {{ .title }}
          </title>
      </head>
      <body>
          {{ .greating }}
      </body>
    
  </html>

Run App engine development server

dev_appserver.py –port=9999 ./The result

Deployment to GAE using gcloud command

gcloud app deploy –verbosity=info

The Final Result On Browser

50.result

Resources: link1

Golang Common Error: missing ‘,’ before newline in composite literal

This is my previous (error) code:

r.GET("/", func(c *gin.Context) {
          c.HTML(http.StatusOK, "main.html", pongo2.Context{
              "title":    "Hello world !",
              "greating": "Hi, Hello world my mate!"
          })
      })  

And here is the changes:

r.GET("/", func(c *gin.Context) {
          c.HTML(http.StatusOK, "main.html", pongo2.Context{
              "title":    "Hello world !",
              "greating": "Hi, Hello world my mate!",
          })
      })

can you find the different ? no ? here is the mitsake:

“greating”: “Hi, Hello world my mate!”,

Yeah the error is because of missing a comma LOL

Golang: Collection of structs

This post intended to be my personal note regarding struct collection (map with structs inside):

Link: https://play.golang.org/p/Tj3cpLu-mu

package main

import (
	"fmt"
)

type Animalia struct {
	fart bool
}

type Person struct {
	Animalia // Embedding as extend replacement
	Name, Address string
}

func (p Person) print() {
	if p.Animalia.fart {
		fmt.Println(p.Name, p.Address)
	}
}

type Human interface {
	print()
}

func humanCheck(h Human) {
}

func main() {
	person := Person{
		Animalia{
			true,
		},
		"Yusuf",
		"Jalan No.90",
	}

	person.print()

	// Collection
	people := map[int]Person{
		1: { Animalia{true}, "jhoni", "Jl. Kehidupan no.90"},
		2: { Animalia{true}, "Rhey", "Jl. Kehidupan no.99"},
		3: { Animalia{true}, "Rhona", "Jl. Girang no.77"},
	}

	for _, person := range people {
		humanCheck(person)
		person.print()
	}
}

Output

structoutput1.png

Google App Engine: Setting up GAE Local Development Server

In this blog post I’m going to show you about install and setting up Google Cloud SDK in Local computer environment for Go / Golang web development, my current computer runs Ubuntu 14.04 as primary os but I’m quite sure that the following steps run on the latest Ubuntu as well.

Requirements :

  1. Ubuntu 14.04 or later
  2. Python 2.7.x or later
  3. Java 1.7 or later
  4. Git

Let’s start the journey :

Download Google Cloud SDK
Download Google Cloud SDK
Extract Google Cloud SDK to local folder
Extract Google Cloud SDK to local folder
Install Google Cloud SDK to local system
Install Google Cloud SDK to local system
Choose yes to improve Google Cloud SDK
Choose yes to improve Google Cloud SDK
Update environment variables / bashrc path
Update environment variables / bashrc path
Update environment variables / bashrc path
Update environment variables / bashrc path
Restart / Reload Bash
Restart / Reload Bash
GCLOUD Init and Login
GCLOUD Init and Login
GCLOUD Init and Login to browser
GCLOUD Init and Login to browser
GCLOUD Init and Login to browser
GCLOUD Init and Login to browser
GCLOUD Init and Login to browser
GCLOUD Init and Login to browser
Give Permission
Give Permission
Give Permission
Give Permission

22.WebLoggedIn

Terminal Logged in
Terminal Logged in
Select Project
Select Project
Check Development App Server
Check Development App Server
Generate ssh Public Key
Generate ssh Public Key
SSH Public Key
SSH Public Key
GitLab Settting
Gitlab Setting
GitLab Settting
GitLab Settting
GitLab SSH Pub key
GitLab SSH Pub key
Install GIT
Install GIT
Clone Code From Repository
Clone Code From Repository
Run development server
Run development server
Runs application
Runs application
Local Web Application
Local Web Application
Admin Server
Admin Server

Golang: Undertanding Basic of Concurrence Programming in the easiest way

In this blog post I’m gonna show you an easy example regarding basic concurrency in Golang programming, it will be an easy step-by-step approach to avoid misapprehend to the example (sample).

What is concurrency?

Concurrency is the composition of independently executing computations.

Concurrency is a way to structure software, particularly as a way to write clean code that interacts well with the real world.

It is not parallelism.

Rob Pike, Google

https://talks.golang.org/2012/concurrency.slide#6

Step 1:

package main

import (
	"fmt"
	"time"
)

func sayHello() {
	for x := 0; x &lt; 10; x++ {
		fmt.Println("Hello ", x)
	}
}

func main() {
	start := time.Now()
	fmt.Println("Start at: ", start)
	fmt.Println("------------------------------------------------------")

	go sayHello()

	fmt.Println("------------------------------------------------------")
	fmt.Println("End at: ", time.Now())
	fmt.Println("Done at: ", time.Now().Sub(start))
}

Output:

basic1

In this  step we do not have output to be printed on the screen from sayHello() because the main process ends before the goroutine being executed. Golang program execution begins by initializing the main package and then invoking the function main. When the function main returns, the program exits. It does not wait for other (non-main) goroutines to complete.

A goroutine is a lightweight thread managed by the Go runtime.

Step 2

func main() {
	start := time.Now()
	fmt.Println("Start at: ", start)
	fmt.Println("------------------------------------------------------")

	go sayHello()

	time.Sleep(time.Second)

	fmt.Println("------------------------------------------------------")
	fmt.Println("End at: ", time.Now())
	fmt.Println("Done at: ", time.Now().Sub(start))
}

Output:

Basic2delay

The example above uses delay to make main function (process) waits the goroutine through 1 second before ends, 1 second is more than enough for Golang to print out all the lines on the screen. for the next step I’m gonna add  delay to the loop inside the sayHello() function so it will takes 9 seconds to be finished (and keep 1 second delay in the main function), let’s see what will take place.

Step 3

func sayHello() {
	for x := 0; x &lt; 10; x++ {
		fmt.Println("Hello ", x)
		time.Sleep(time.Second)
	}
}

func main() {
	start := time.Now()
	fmt.Println("Start at: ", start)
	fmt.Println("------------------------------------------------------")

	go sayHello()

	time.Sleep(time.Second)

	fmt.Println("------------------------------------------------------")
	fmt.Println("End at: ", time.Now())
	fmt.Println("Done at: ", time.Now().Sub(start))
}

Output:

Basic3

Each loop waits for 1 second after printed the text, so in 1 minute it adequate to print 2 lines.

Step 4 Make main function wait goroutine to be done before it ends

We can make main function to wait all goroutines to be done before end it’s process without using primitive delay (time.Sleep(time.Second)) function.

package main

import (
	"fmt"
	"time"
	"sync"
)

func sayHello(wg *sync.WaitGroup) {
	for x := 0; x &lt; 10; x++ {
		fmt.Println("Hello ", x)
		time.Sleep(time.Second)

		if x == 8 {
			wg.Done()
		}

	}
}

func main() {
	var wg sync.WaitGroup

	start := time.Now()
	fmt.Println("Start at: ", start)
	fmt.Println("------------------------------------------------------")

	wg.Add(1)
	go sayHello(&wg)

	wg.Wait()

	fmt.Println("------------------------------------------------------")
	fmt.Println("End at: ", time.Now())
	fmt.Println("Done at: ", time.Now().Sub(start))
}

Output:

basic5

Please pay attention to these lines:

var wg sync.WaitGroup
wg.Add(1) // Should be called before goroutine call
wg.Done()
wg.Wait()

Step 5 Go channel

Channels are the pipes that connect concurrent goroutines. You can send values into channels from one goroutine (ch<-) and receive those values into another goroutine (<-ch).

I don’t want to talk much about channel to avoid confusion, instead I will just show you several behavior of channel.

Receive operation <-ch in the main

package main

import (
	"fmt"
)

func sayHello(msg chan string) {

	msg <- "Hello Lorem ipsum1!"
	msg <- "Hello Lorem ipsum2!"
	msg <- "Hello Lorem ipsum3!"
	close(msg)
}

func main() {
	msg := make(chan string)

	go sayHello(msg)

	for value := range(msg) {
		fmt.Println(value);
	}

	// Or you could just uncomment following lines (but comments 'for' lines first)
	// fmt.Println("1:", <-msg)
	// fmt.Println("2:", <-msg)
	// fmt.Println("3:", <-msg)
	// fmt.Println("4:", <-msg) // empty or error if you did't close(msg)
}

Output:

channelx1

package main

import (
      "fmt"
)

func sayHello(msg chan string) {
      says := "Says: "
      for x := 0; x < 10; x++ {
          says += fmt.Sprintf("Hello-%d ,", x)
          fmt.Println("ok")
      }   

      msg <- says
}

func main() {
      msg := make(chan string)
      go sayHello(msg)
      fmt.Println(<-msg)
}

Output:

channel1

Receive operation (<-msg) in main() is a blocking operation, so the program won’t continue until a value is available.

Receive operation <-ch from goroutine to be printed by function

package main

import (
	"fmt"
)

func sayHello(msg chan string) {

	msg <- "hello world"

}

func printHello2(msg string) {
	fmt.Println(msg)

}

func main() {

	msg := make(chan string)

	go sayHello(msg)

	printHello2(<-msg)

}

Output:

exited1

Select

package main

import (
	"fmt"
	"time"
)

func main() {

	// For our example we'll select across two channels.
	c1 := make(chan string)
	c2 := make(chan string)

	go func() {
		time.Sleep(time.Second * 1)
		c1 <- "one"
	}()

	go func() {
		time.Sleep(time.Second * 2)
		c2 <- "two"
	}()

	for {

		select {
		case msg1, ok := <-c1:
			if ok {
				fmt.Println("received", msg1)
			}
		case msg2, ok := <-c2:
			if ok {
				fmt.Println("received", msg2)
			}
		case <-time.After(3000 * time.Millisecond):
			fmt.Printf("Timed out!")
			return
		}

	}
}

Output:

timeout

The select statement lets a goroutine wait on multiple communication operations.

A select blocks until one of its cases can run, then it executes that case. It chooses one at random if multiple are ready.