Installing and Unistalling SpaceVim / SpaceVim on UBuntu Linux (Or just Linux in General)

I just Installed /SpaceVim on my Ubuntu 14.04 Laptop but I realized that It was wrong because my VIM IDE turned to unfamiliar IDE for me, and the worse case was my previous setting and configurations in my .vimrc are no longer working.

Here is the SpaceVim installation step:

curl -sLf https://spacevim.org/install.sh | bash

spacevim

Installation result (Open the VIM first), beautiful but I think it’s better to use it later…

spacevim100

And here is how to uninstall / remove  SpaceVim from your LInux Computer:

curl -sLf https://spacevim.org/install.sh | bash -s — uninstall

remove-spaceVIM

Finally I get my standard VIM back…

myvim1.png

Advertisements

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 < 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 < 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 < 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.

Golang: Debuging Go code

In this post I will show you some easy (tricky) ways to debug go code (forget about gdb, it’s too hard to understand).

Print Struct as string

package main

import (
    "fmt"
)

type Person struct {
    Name, Address       string
    Age, Height, Weight int
}

func main() {
    Yusuf := Person{
        Name:    "Yoesoff",
        Address: "Bandung Barat",
        Age:     17,
        Height:  165,
        Weight:  70,
    }   

    fmt.Println(Yusuf) // Print struct as string
}

Output:

{Yoesoff Bandung Barat 17 165 70}
{Yoesoff Bandung Barat 17 165 70}

Use fmt.Printf to find Variable / Object type in Golang

fmt.Printf("%T\n",v)
package main

import (
    "fmt"
)

type Person struct {
    Name, Address       string
    Age, Height, Weight int
}

func main() {
    Yusuf := Person{
        Name:    "Yoesoff",
        Address: "Bandung Barat",
        Age:     17,
        Height:  165,
        Weight:  70,
    }   

     fmt.Printf("%T\n", A)
     fmt.Printf("%T\n", B)
     fmt.Printf("%T\n", Yusuf)
}

Output:

string
int
main.Person

Use reflect to find Variable / Object type in Golang

package main

import (
	"fmt"
	"reflect"
)

type Person struct {
	Name, Address       string
	Age, Height, Weight int
}

func main() {
	Yusuf := Person{
		Name:    "Yoesoff",
		Address: "Bandung Barat",
		Age:     17,
		Height:  165,
		Weight:  70,
	}

	Yuka := &Person{
		"Zha",
		"Bandung Barat",
		16,
		165,
		60,
	}

	A := 10
	B := 12
	C := A + B
	D := "I AM A BATMAN"

	fmt.Println(reflect.TypeOf(Yusuf))
	fmt.Println(reflect.TypeOf(Yuka))
	fmt.Println(reflect.TypeOf(*Yuka))
	fmt.Println(reflect.TypeOf(C))
	fmt.Println(reflect.TypeOf(D))
}

Output:

main.Person
*main.Person
main.Person
int
string

Using go-spew to debug Go data structures (Object and Variable)

Installation

$ go get -u github.com/davecgh/go-spew/spew

  package main

  import (
      "github.com/davecgh/go-spew/spew"
  )

  type Person struct {
      Name, Address       string
      Age, Height, Weight int
  }

  func main() {
      Yusuf := Person{
          Name:    "Yoesoff",
          Address: "Bandung Barat",
          Age:     17,
          Height:  165,
          Weight:  70,
      }

      A := 10
      B := 12
      C := A + B
      D := "Wakwau Golang si Golang"

      spew.Dump(Yusuf)
      spew.Dump(C)
      spew.Dump(D)
  }

Output:

(main.Person) {
Name: (string) (len=7) “Yoesoff”,
Address: (string) (len=13) “Bandung Barat”,
Age: (int) 17,
Height: (int) 165,
Weight: (int) 70
}
(int) 22

(string) (len=23) “Wakwau Golang si Golang”

Using godebug A cross-platform debugger for Go.

Installation:

$ go get github.com/mailgun/godebug

1. install-godebug

Setup Go executable binary path :

2. Setup Go Binari

3. Go-Debug-Running

Insert a breakpoint anywhere in a source file you want to debug:

_ = "breakpoint"

package main

  import "fmt"

  type Person struct {
      Name, Address       string
      Age, Height, Weight int
  }

  func main() {
      Yusuf := Person{
          Name:    "Yoesoff",
          Address: "Bandung Barat",
          Age:     17,
          Height:  165,
          Weight:  70,
      }

      _ = "breakpoint"

      A := 10
      B := 12
      C := A + B
      D := "Wakwau Golang si Golang"
      _ = "breakpoint"

      fmt.Println(A)
      fmt.Println(B)
      fmt.Println(C)
      fmt.Println(D)
      fmt.Println(Yusuf)
  }

Output:

6.Godebugx

Task Scheduling using Laravel 5

In this blog post I will show you an easy example about how to create a simple Laravel 5 Command-Line program to be executed regularly by particular time interval or specified time.

Laravel’s command scheduler allows you to fluently and expressively define your command schedule within Laravel itself. When using the scheduler, only a single Cron entry is needed on your server. Your task schedule is defined in the app/Console/Kernel.php file’s schedule method. To help you get started, a simple example is defined within the method.

Create Simple Laravel Command-Line

we will use two log files to store output text (I show you my real path to avoid confuse ) :

Cron log file :  /home/yusuf/general.log

Scheduler output file: /home/yusuf/htdocs/blog/storage/logs/scheduler.log

 

  • Create app/Console/Commands/SayHello.php.

    1. CreateSayHelloDotPHP
    Create a new PHP file
  • Let’s write the simple short php code

    vim app/Console/Commands/SayHello.php

    namespace App\Console\Commands;
    
      use Illuminate\Console\Command;
    
      class SayHello extends Command
      {
          protected $signature = 'sayhello';
          protected $description = 'Say Hello User';
    
          public function __construct()
          {
              parent::__construct();
          }
    
          public function handle()
          {
              $this->info('Hello user...! ('. date("Y-m-d H:i:s") .') ');
          }
      }
    

    vim app/Console/Commands/Kernel.php

     /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
     protected $commands = [
         Commands\SayHello::class
     ];
    
  • Check out the new registered Laravel 5 Command-Line in the command list

    3. checkNewCommand
    New registered Laravel Command-line
  • Execute SayHello as Laravel 5 Command-Line program

    4. ExecuteIt
    Execute command line on terminal

Create Scheduler to Execute Command-Line

The next important step is about how to execute our Laravel Command-Line regularly. By using the Laravel scheduler you only need to add the following short line Cron entry to your server an then Laravel Forge will manage the Cron entries for you:

  • Create a log file for our Laravel scheduler storage/logs/scheduler.log:5. Laravel-Logs
  • Edit app/Console/Commands/Kernel.php   

    vim app/Console/Commands/Kernel.php

  • /**
     * Define the application's command schedule.
     *
     * @param \Illuminate\Console\Scheduling\Schedule $schedule
     * @return void
     */
     protected function schedule(Schedule $schedule)
     {
         $filePath = '/home/yusuf/htdocs/blog/storage/logs/scheduler.log';
         $schedule->command('sayhello')->everyMinute()->sendOutputTo($filePath);
     }
    
  • Test execute the scheduler

    ./artisan schedule:run6.scheduler-registered

  • Register schedule command as cron entry

    $ crontab -e7.registerCronEntry.png

     

    CronTabBlog1

  • Save the file and quit text editor
  • Tail the log file to see the result

    tail -f ~/general.log

    tail -f /home/yusuf/htdocs/blog/storage/logs/scheduler.log

    CrontabOutput2

  • That’s all

Golang: Simple Golang Web Application

In this post I’d like to show you a simple web application developed using Go / Golang that demonstrate you several basic web  functionalities such as http request and http response.

The result should be something like this that runs in your web browser:

golang-x1golang-x2

main.go

package main

  import (
      "fmt"
      "html/template"
      "net/http"
      "os"
  )

  type FormData struct {
      Name    string
      Country string
  }

  func form(w http.ResponseWriter, r *http.Request) {
      folderpath, _ := os.Getwd() // Get current folder location
      form, _ := template.ParseFiles(folderpath + "/form.gtpl")
      form.Execute(w, nil)
  }

  func view(w http.ResponseWriter, r *http.Request) {
      if r.Method == "POST" {

          // Process form data
          r.ParseForm()
          name := r.Form["name"][0] // Yes, Go able to receive more than one name
          country := r.Form["country"][0]
          //fmt.Println(name)
          //fmt.Println(country)

          data := FormData{
              Name:    name,
              Country: country,
          }   

          folderpath, _ := os.Getwd() // Get current folder location
          view, _ := template.ParseFiles(folderpath + "/view.gtpl")
          view.Execute(w, data)
      } else {
         fmt.Fprintf(w, "Method not allowed!") // write data to response
      }
  }

  func main() {
      // Routes
      http.HandleFunc("/", form)
      http.HandleFunc("/view", view)

      http.ListenAndServe(":8000", nil) // setting listening port
  }

form.gtpl

<html>

      <body>
<h1>Hallo World</h1>
<form method="POST" action="/view">
              Name : <input name="name" />
              Coutry : <input name="country" />

              <input type="submit" />
          </form>

      </body>

  </html>

view.gtpl

 <html>
  <head>
  <title></title>
  </head>
      <body>
          Hello World 

          Welcome -{{.Name}}- from -{{.Country}}-
      </body>
  </html>

Build and Run The code

golang x3

Code Snapshot

VIMX1VIMX2VIMX3

Golang: Catatan belajar OOP dengan GO language

Source code di sini

Struct pada Golang adalah kumpulan dari fields dan atau properties. Struct berbeda dengan Class pada Java misalnya, karena salah satunya Struct pada Go menggunakan Composition bukan inheritance yaitu dengan meng-compose sebuah struct di struct lainnya sehingga fields dan atau properties dari sebuat struct ada di dalam struct lain yang meng-embed struct tersebut.

Composition atau juga bisa di sebut embedding bisa dikatakan sebagai pengganti penggunaan inheritance di GO ,  untuk menjadikan gambaran yang lebih jelas saya mencoba mempraktekan perilaku tersebut dalam beberapa baris bahasa GO di bawah ini, selain Composition/embedding disini juga terdapat penggunaan interface sederhana, berikut kodenya:


package main

import (
	f "fmt"
	s "strconv"
)

// 1. Manusia pada umumnya
type Manusia struct {
	nama                     string
	umur                     int
	telepon, hanphone, email string
}

// 2. Manusia berstatus Mahasiswa
type Mahasiswa struct {
	Manusia       // Embed Manusia / semacam extend di OOP Java
	kampus, kosan string
	uang_jajan    float32
}

// 3. Mahasiswa berstatus Karyawan
type Karyawan struct {
	Manusia            // Embed Manusia / semacam extend di OOP Java
	perusahaan, asrama string
	salary             float32
}

// Semua manusia pasti makan
func (m Manusia) makan(makanan string) {
	f.Printf("Makan: %s \n", makanan)
}

// Semua manusia pasti minum
func (m Manusia) minum(minuman string) {
	f.Printf("Minum: %s \n", minuman)
}

// Semua mahasiswa pasti ujian
func (m Mahasiswa) ujian(mata_kuliah string) {
	f.Printf("Ujian mata kuliah: %s \n", mata_kuliah)
}

// Semua karyawan pasti gajian
func (k Karyawan) gajian() {
	f.Printf("Gajian: $.%e ", k.salary)
}

// Interfaces untuk yang pelajar
type Pelajar interface {
	makan(makanan string)
	minum(minuman string)
	ujian(mata_kuliah string)
}

type Pekerja interface {
	makan(makanan string)
	minum(minuman string)
	gajian()
}

// Fungsi Di luar interface / ga ada di interface
func (m *Manusia) bikinHoax() {
	f.Printf(m.nama + " Bikin berita Hoax biar kacau...... ")
}

// Main function
func main() {
	f.Println(`بِسْــــــــــــــــــمِ اللهِ الرَّحْمَنِ الرَّحِيْمِ`)

	// Implementasi Struct di atas adalah sebagai berikut

	// 1. Mahasiswa
	joni := Mahasiswa{Manusia{"Joni", 20, "0214353454", "098342342", "jonni@example.com"}, "Prapatan University", "Kosan ibu Nuri", 200}
	var joni_mahasiswa Pelajar // Interface
	f.Println(`Nama:` + joni.nama)
	f.Println(`Nama:` + s.Itoa(joni.umur))
	f.Println(`Nama:` + joni.telepon)
	f.Println(`Nama:` + joni.hanphone)
	f.Println(`Nama:` + joni.email)

	joni_mahasiswa = joni
	joni_mahasiswa.makan(`Nasi padang`)
	joni_mahasiswa.minum(`Es Campur`)
	joni_mahasiswa.ujian(`Sastra Rusia`)
	// Kalo tidak di komen fungsi di bawah akan ada error: joni_mahasiswa.bikinHoax undefined (type Pelajar has no field or method bikinHoax)
	// joni_mahasiswa.bikinHoax() // Ya iya lah masa Mahasiswa bikin Hoax, bego di piara mah ga usah kuliah...
	joni.bikinHoax() //Joni sebagai manusia bisa khilaf, jadi ini akan jalan

	f.Println(``)
	f.Println(`------------------------`)
	f.Println(``)

	// 2. Karyawan
	kokom := Karyawan{Manusia{"Kokom Komariyah", 27, "02199999", "0888902343", "kokok-cute@example.com"}, "PT. Telekokom Tbbek", "Rumah dinas Telekokom", 20000}
	var kokom_karyawan Pekerja // Interface
	f.Println(`Nama:` + kokom.nama)
	f.Println(`Nama:` + s.Itoa(kokom.umur))
	f.Println(`Nama:` + kokom.telepon)
	f.Println(`Nama:` + kokom.hanphone)
	f.Println(`Nama:` + kokom.email)

	kokom_karyawan = kokom
	kokom_karyawan.makan(`Steak Kerbau Buleng`)
	kokom_karyawan.minum(`Vodka Bridenk`)
	kokom_karyawan.gajian()
	f.Println(``)
	// Kalo tidak di komen fungsi di bawah akan ada error: kokom_karyawan.bikinHoax undefined (type Pelajar has no field or method bikinHoax)
	// kokom_karyawan.bikinHoax() // masa Professional bikin Hoax...
	kokom.bikinHoax() //Kokom sebagai manusia bisa khilaf, jadi ini jalan
	f.Println(``)
	f.Println(`----------------------------
	f.Println(``)
	// Manusia biasa
	manusia := Manusia{"Entahlah", 27, "02100", "08000", "teuing@ex.com"}
	f.Println(`Nama:` + manusia.nama)
	f.Println(`Nama:` + s.Itoa(manusia.umur))
	f.Println(`Nama:` + manusia.telepon)
	f.Println(`Nama:` + manusia.hanphone)
	f.Println(`Nama:` + manusia.email)
	manusia.bikinHoax() // Wajar namanya manusia
}

Golang: Error Handling in Go Programming

Return Error


package main

import (
	"errors"
	"fmt"
)

func sum(a int, b int) (int, error) {

	x := a + b

	if a < 0 || b < 0 {
		return x, errors.New("value should be greater than zero")
	}

	return x, nil
}

func main() {
	a := 12
	b := -1

	x, err := sum(a, b)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(x)
}

Panic and Recover from panic

package main

import (
	//	"errors"
	"fmt"
)

func sum(a int, b int) (int, error) {
	defer func() {
		if err := recover(); err != nil {
			fmt.Println("Wow Error: ", err)
		}
	}()

	x := a + b

	if a < 0 || b < 0 {
		panic("I am panicking kaka")
	}

	return x, nil
}

func main() {
	a := 12
	b := -1

	x, err := sum(a, b)

	fmt.Println(x)
	fmt.Println(err)
}

Golang: Structs Methods and Interface

Go Struct, Methods and Embedding example

package main

import "fmt"

type User struct {
	name, address string
}

type Employee struct {
	level  int
	salary int
	user   User // Embedding
}

/**
 * Greating for User
 */
func (u User) Greeting() string {
	return fmt.Sprintf("Dear %s %s", u.name, u.address)
}

/**
 * Greating for User
 */
func (e Employee) Greeting() string {
	return fmt.Sprintf("Your level %d, Your salary %d", e.level, e.salary)
}

func main() {
	user := User{"Yazid", "Jalan munukan no.19"}

	fmt.Println(user.Greeting())

	employee := Employee{1, 2000, user}

	fmt.Println(employee.user.Greeting())
	fmt.Println(employee.Greeting())
}

Using Pointer to keep the lowest amount of structs per file

package main

import (
	"fmt"
)

type User struct {
	FirstName, LastName string
}

func (u *User) Greeting() string {
	return fmt.Sprintf("Dear %s %s", u.FirstName, u.LastName)
}
func main() {
	u := &User{"Yusuf", "Ibrahim"}
	fmt.Println(u.Greeting())
}

Golang Interface Example

package main

import (
	"fmt"
)

type User struct {
	FirstName, LastName, address string
}

func (u *User) Name() string {
	return fmt.Sprintf("%s %s", u.FirstName, u.LastName)
}

func (u *User) Address() string {
	return fmt.Sprintf("%s", u.address)
}

type Customer struct {
	Id                int
	FullName, address string
}

func (c *Customer) Name() string {
	return c.FullName
}

func (c *Customer) Address() string {
	return fmt.Sprintf("%s", c.address)
}

type Namer interface {
	Name() string
	Address() string
}

func Greet(n Namer) string {
	return fmt.Sprintf("Dear %s at %s", n.Name(), n.Address())
}
func main() {
	u := &User{"Matt", "Aimonetti", "jalan Banda"}
	fmt.Println(Greet(u))

	c := &Customer{42, "Francesc", "jalan Banda"}
	fmt.Println(Greet(c))
}

Golang: Map, Hashes, Dictionary

Golang Maps are similar to what other language call ‘dictionaries’ or ‘hashes’.

 

package main

import "fmt"

var names = map[string]int{"Katrina": 10, "Evan": 11, "Neil": 12}

func main() {
    //  fmt.Printf("%#v", names)

    for x, _ := range names {
        fmt.Println(x)
    }
    // Output:
    // Katrina
    // Evan
    // Neil

    for _, name := range names {
        fmt.Println(name)
    }
    // Output:
    // 10
    // 11
    // 12

    for x, name := range names {
        fmt.Printf("%s : %d \n", x, name)
    }
    // Output
    // Katrina : 10
    // Evan : 11
    // Neil : 12

}

Add / Append new data to a map


package main

import "fmt"

func main() {
	names := make(map[string]int)
	//	fmt.Printf("%#v", names)

	names["jhony"] = 1
	names["beta"] = 2

	for x, name := range names {
		fmt.Printf("%s : %d \n", x, name)
	}

}

Collection of objects


package main

import "fmt"

type Person struct {
	name, address string
}

func main() {
	jelemas := make(map[string]Person)

	jelemas["Yoesoff"] = Person{"yusuf", "Jl. Wakwau no.09"}
	jelemas["Yuka"] = Person{"gokana", "Jl. Wakwau no.90"}
	jelemas["Mumun"] = Person{"pocong", "Jl. Wakwau no.99"}

	//	fmt.Printf("%#v", names)
	for x, wong := range jelemas {
		fmt.Printf("%s : %s (%s) \n", x, wong.name, wong.address)
	}

	// Output
	// Yuka : gokana (Jl. Wakwau no.90)
	// Mumun : pocong (Jl. Wakwau no.99)
	// Yoesoff : yusuf (Jl. Wakwau no.09) 

}

Remove / Delete item from map


package main

import "fmt"

type Person struct {
	name, address string
}

func main() {
	jelemas := make(map[string]Person)

	jelemas["Yoesoff"] = Person{"yusuf", "Jl. Wakwau no.09"}
	jelemas["Yuka"] = Person{"gokana", "Jl. Wakwau no.90"}
	jelemas["Mumun"] = Person{"pocong", "Jl. Wakwau no.99"}

	//	fmt.Printf("%#v", names)
	for x, wong := range jelemas {
		fmt.Printf("%s : %s (%s) \n", x, wong.name, wong.address)
	}

	// Output
	// Yuka : gokana (Jl. Wakwau no.90)
	// Mumun : pocong (Jl. Wakwau no.99)
	// Yoesoff : yusuf (Jl. Wakwau no.09)

	delete(jelemas, "Mumun")

	fmt.Println("Delete Mumun (Because he is scary)")
	for x, wong := range jelemas {
		fmt.Printf("%s : %s (%s) \n", x, wong.name, wong.address)
	}

	// Output
	// Delete Mumun (Because he is scary)
	// Yoesoff : yusuf (Jl. Wakwau no.09)
	// Yuka : gokana (Jl. Wakwau no.90)

}

Check Element existence in a map


	// Check Element
	_, ok := jelemas["Yuka"]

	fmt.Println(ok)

	_, ok = jelemas["Mumun"]

	fmt.Println(ok)