After several months of switching from Java to Golang, it seemed to me that
it would be interesting to make the translation of Java code into Golang 
automatically.
The text below shows what has been done so far.

The work is not a prototype, but rather indicates the possibility of 
achieving a result.
Therefore, I deliberately simplify the development context of the Converter 
where it was 
possible. 

At first it seemed important to me that between Java and Go there is a 
difference 
between the implementation of the Dynamic Dispatching, more precisely, 
there is no 
Dynamic Dispatching in Go. The applied solution in the current 
implementation 
looks not only ugly but even violates the several very important rules of 
the OO design, 
I'm not kidding here. But this option looks like that it will be working. 

Onward I will provide the 4 code samples in Java, followed by the 
automatically 
generated Golang code and comments as needed. 

*1. Of course, I started with the most popular program: "Hello World".*

package main;

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

*Converter gave out:*

package main

import (
        "fmt"
        "os"
)

type HelloWorld struct{}

func main() {

        var args []string = os.Args

        var hw HelloWorld = HelloWorld{}
        hw.HelloWorld_main(args)
}

/** generated method **/
func (helloWorld *HelloWorld) HelloWorld_main(args []string) {
        fmt.Println("Hello World")
}

*2. Next, it was interesting to deal with the problem of a simple 
inheritance.*

package main;

public class TestInheritance {
  public static void main(  String[] args){
    Inheritance inh=null;
    inh=new Second();
    inh.hello();
    inh=new Third();
    inh.hello();
  }
}
public interface Inheritance {
  public void hello();
}
class Second implements Inheritance {
  public void hello(){
    System.out.println("Second");
  }
}
class Third implements Inheritance {
  public void hello(){
    System.out.println("Third");
  }
}
 
*Converter gave out:*

package main

import (
        "fmt"
        "os"
)

type TestInheritance struct{}

func main() {

        var args []string = os.Args

        var ti TestInheritance = TestInheritance{}
        ti.TestInheritance_main(args)
}

/** generated method **/
func (testInheritance *TestInheritance) TestInheritance_main(args []string) 
{

        var inh Inheritance
        inh = AddressSecond(Second{})
        inh.hello()
        inh = AddressThird(Third{})
        inh.hello()
}

type Inheritance interface {
        hello()
}
type Second struct{}

func (second *Second) hello() {
        fmt.Println("Second")
}

type Third struct{}

func (third *Third) hello() {
        fmt.Println("Third")
}

func AddressSecond(s Second) *Second { return &s }
func AddressThird(t Third) *Third    { return &t }


*3. In the following example, it is necessary to correctly define    a 
common interface for the inheritance tree.*

package no.packeges;

public class TestExtension {
  public static void main(  String[] args){
    TestExtension te=new TestExtension();
    te.hello();
    te=new Second();
    te.hello();
    te=new Third();
    te.hello();
    te=new Fourth();
    te.hello();
  }
  public void hello(){
    System.out.println("hello");
  }
}
class Second extends TestExtension {
  public void hello(){
    System.out.println("Second");
  }
}
class Third extends TestExtension {
  public void hello(){
    System.out.println("Third");
  }
}
class Fourth extends Third {
  public void hello(){
    System.out.println("Fourth");
  }
}

*Converter gave out:*

package main

import (
        "fmt"
        "os"
)

type TestExtension struct{}

func main() {

        var args []string = os.Args

        var te TestExtension = TestExtension{}
        te.TestExtension_main(args)
}
func (testExtension *TestExtension) hello() {
        fmt.Println("hello")
}

/** generated method **/
func (testExtension *TestExtension) TestExtension_main(args []string) {

        var te ITestExtension = AddressTestExtension(TestExtension{})
        te.hello()
        te = AddressSecond(Second{})
        te.hello()
        te = AddressThird(Third{})
        te.hello()
        te = AddressFourth(Fourth{})
        te.hello()
}

type Second struct {
        TestExtension
}

func (second *Second) hello() {
        fmt.Println("Second")
}

type Third struct {
        TestExtension
}

func (third *Third) hello() {
        fmt.Println("Third")
}

type Fourth struct {
        Third
}

func (fourth *Fourth) hello() {
        fmt.Println("Fourth")
}

type ITestExtension interface { 
/** Generated Method */
        hello()
}

func AddressSecond(s Second) *Second                      { return &s }
func AddressThird(t Third) *Third                         { return &t }
func AddressTestExtension(t TestExtension) *TestExtension { return &t }
func AddressFourth(f Fourth) *Fourth                      { return &f }





*4. Now the Dynamic Dispatching absence. The problematic method in    the 
last sample is the amount() in class Repeater.    Without rewriting the 
code, I think it would be impossible to    invoke it correctly.*

package main;

public class Speaker {
  String message;
  public static void main(  String[] args){
    Speaker sp = new Speaker("Say hello !");
    System.out.println(sp.amount());
    sp.speak();
    Repeater rp = new Repeater("Say hello !",3);
    System.out.println(rp.amount());
    rp.speak();
  }
  public Speaker(  String message){
    this.message=message;
  }
  public void speak(){
    for (int i=0; i < amount(); i++) {
      System.out.println(this.message);
    }
  }
  public int amount(){
    return 1;
  }
}
class Repeater extends Speaker {
  int to_repeat=0;
  public Repeater(  String message,  int amount){
    super(message);
    this.to_repeat=amount;
  }
  public int amount(){
    return this.to_repeat;
  }
}

*Converter gave out:*

package main

import (
        "fmt"
        "os"
)

type Speaker struct {
        message string
}

func main() {

        var args []string = os.Args

        var s_dummy Speaker = NewSpeaker("")
        s_dummy.Speaker_main(args)
}
func NewSpeaker(message string) Speaker {

        var speaker Speaker = Speaker{message}
        return speaker
}
func (speaker *Speaker) speak() {
        for i := 0; i < speaker.amount(); i++ {
                fmt.Println(speaker.message)
        }
}
func (speaker *Speaker) amount() int {
        return 1
}

/** generated method **/
func (speaker *Speaker) Speaker_main(args []string) {

        var sp ISpeaker = AddressSpeaker(NewSpeaker("Say hello !"))
        fmt.Println(sp.amount())
        sp.speak()

        var rp ISpeaker = AddressRepeater(NewRepeater("Say hello !", 3))
        fmt.Println(rp.amount())
        rp.speak()
}

type Repeater struct {
        Speaker

        to_repeat int
}

func NewRepeater(message string, amount int) Repeater {

        var repeater Repeater = Repeater{NewSpeaker(message), amount}
        return repeater
}
func (repeater *Repeater) amount() int {
        return repeater.to_repeat
}
func (repeater *Repeater) speak() {
        for i := 0; i < repeater.amount(); i++ {
                fmt.Println(repeater.message)
        }
}

type ISpeaker interface { 

/** Generated Method */

        amount() int
/** Generated Method */

        speak()
}

func AddressRepeater(r Repeater) *Repeater { return &r }
func AddressSpeaker(s Speaker) *Speaker    { return &s }




*5. I will be thankful for any feedback. Thanks to everyone.*

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/aeff3481-9a4b-4f5e-b1b2-de4d28a12a0fn%40googlegroups.com.

Reply via email to