Rick

Rick
Rick

Monday, January 14, 2013

Java Guide to Go Programming article 1: Class and Encapsulation

I don't know if this is going to be a one time thing or something that I do on a regular basis. I thought it would be instructive to compare/contrast Go Programming to a mainstream language like Java. What follows is Java code listings followed by Go Programming listings. When possible I try to Idioms and code conventions adopted by those languages. To be fair on LOC, I use the built-in code formatting support of Eclipse.

I used the Goclipse Eclipse plugin and Java Eclipse IDE to format the code. I start with hello world and then I create a simple Employee class to show encapsulation and grouping behavior and data in the two languages.

Hello World! Java

/java-examples/src/com/example/Hello.java
package com.example;

public class Hello {

 public static void main(String[] args) {
  System.out.println("Hello World!");
 }
}

Now the same in Go Programming.
package main

import "fmt"

func main() {
 fmt.Println("Hello World!")
}

I had to get hello world out of the way. :)

Idiomatic Java class with getters and setters (properties) for Employee

/java-examples/src/com/example/encapsulation/Employee.java
package com.example.encapsulation;

public class Employee {
 private String firstName;
 private String lastName;
 private int age;
 private boolean current;

 public Employee(String firstName, String lastName, int age, boolean current) {
  this.firstName = firstName;
  this.lastName = lastName;
  this.age = age;
  this.current = current;
 }

 public String getFirstName() {
  return firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public int getAge() {
  return age;
 }

 public boolean isCurrent() {
  return current;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public void setAge(int age) {
  this.age = age;
 }

 public void setCurrent(boolean current) {
  this.current = current;
 }

 @Override
 public String toString() {
  return "Employee [firstName=" + firstName + ", lastName=" + lastName
    + ", age=" + age + ", current=" + current + "]";
 }

}

Idiomatic Go Programming class equivalent with getters and setters (properties) for Employee

/examples/src/example/encapsulation/employee.go
package encapsulation

import "strconv"

type Employee struct {
 firstName string
 lastName  string
 age       int
 current   bool
}

func NewEmployee(firstName string, lastName string, age int, current bool) *Employee {
 return &Employee{firstName, lastName, age, current}
}

func (this *Employee) FirstName() string {
 return this.firstName
}

func (this *Employee) LastName() string {
 return this.lastName
}

func (this *Employee) Age() int {
 return this.age
}

func (this *Employee) Current() bool {
 return this.current
}

func (this *Employee) SetFirstName(name string) {
 this.firstName = name
}

func (this *Employee) SetLastName(name string) {
 this.lastName = name
}

func (this *Employee) SetAge(newAge int) {
 this.age = newAge
}

func (this *Employee) SetCurrent(current bool) {
 this.current = current
}

func (this *Employee) String() string {
 return "Employee [firstName=" + this.firstName + ", lastName=" + this.lastName +
  ", age=" + strconv.Itoa(this.age) + ", current=" + strconv.FormatBool(this.current) + "]"
}

Note that I used the variable this but it is more common to use things like emp or e. I used this to make it more clear to Java programmers what I was doing.

Using Java Employee class

/java-examples/src/com/example/UseEncapsulation.java
package com.example;

import com.example.encapsulation.Employee;

public class UseEncapsulation {

 public static void main(String[] args) {
  Employee employee = new Employee("Rick", "Hightower", 32, true);
  System.out.println("" + employee);

  employee.setLastName("Smith");
  employee.setAge(45);

  System.out.printf("Name = %s, %s | Age = %d \n",
    employee.getFirstName(), employee.getLastName(),
    employee.getAge());
 }
}

Using Go Programming struct (aka class)

examples/src/useEncapsulation.go
package main

import (
 "example/encapsulation"
 "fmt"
)

func main() {
 employee := encapsulation.NewEmployee("Rick", "Hightower", 32, true)
 fmt.Println(employee)

 employee.SetLastName("Smith")
 employee.SetAge(45)

 fmt.Printf("Name = %s, %s | Age = %d \n", employee.FirstName(), employee.LastName(), employee.Age())

}

The output for both are the same
Employee [firstName=Rick, lastName=Hightower, age=32, current=true]
Name = Rick, Smith | Age = 45 
More to come....
Kafka and Cassandra support, training for AWS EC2 Cassandra 3.0 Training