Java programmer touched Go language (Implement Java inheritance in Go language)

image

My name is keita69sawada.

Java programmer tried to touch Go language (for the time being), followed by studying Go language.

When I designed it in UML in the same way as Java class design, I couldn't imagine how to realize common processing (abstract method) because there is no "extends" in Go language **. .. ..

Then, "Let's implement a simple Java class design in Go language!" This article was uni-uni.

Target audience

Prerequisites

Introduction

Let's replace a simple sample using Java extensions with Go language. Let's implement the "squeal" method in the classes "dog" and "cat" that inherit "animal".

1. Class diagram

It's not strict UML, but is it like this?

image

3. When trying to achieve the same thing in Go language ...

Below, I was worried (; ´Д `)

  1. How to split the Go language source file? (No concept of class ...)
  1. Since there is no concept of inheritance in Go language, how should common processing (abstract method) be realized?

I thought about these worries in my own way.

4. [Trouble # 1] How should I divide the Go language source file? (No concept of class ...)

I compared the Java source file structure with the Go language source file structure.

4-1. Java source file structure

When a Java programmer looks at the class diagram above and decides on the source file structure, I think that it is usually one class and one file.

file organization


└─src
    ├─animal                         //java package unit folder
    │      Animal.java               //java class unit
    │      Cat.java
    │      Dog.java
    │
    └─main
            Main.java

4-2. Go language source file structure

It has the same structure as the Java source file structure. This time ** dare ** the same.

file organization


└─src
    ├─animal                         //go package unit folder
    │      Animal.go                 //go has no concept of class
    │      Cat.go
    │      Dog.go
    │
    └─main
            Main.go

There is no concept of classes in the Go language, so there is no need to ** split the three (Animal.go, Cat.go, Dog.go) source files. ** (However, the packages must be the same)

For example, it is grammatically OK to combine them into one file as follows. (Grammatically ...)

file organization


└─src
    ├─animal 
    │      Animal_Cat_Dog.go   //Grammatically OK even if you put them together in one source file
    │
    └─main
            Main.go

4-3. Summary of "Trouble 1"

** My own answer ** to "What should I do with Go language source file division? (Class concept or ...)" is as follows.

5. [Trouble # 2] Since there is no concept of inheritance in Go language, how should common processing (abstract method) be realized?

5-1. Common processing (abstract method)

First, let's take a look at the Java source. Animals implement the behavior of bark with abstract methods.

Animal.java


package animal;

public abstract class Animal {
    public abstract void bark();
}

And the source of Go language. The Go language doesn't have abstract methods, so instead it implements the behavior of barking at the interface.

** "There is no abstract concept (alternative to class) of animals" **. ..

Animal.go


package animal

type Barker interface {
	Bark(string)
}

Also, it seems that when the beginning is capitalized, it becomes public scope.

5-2. Concrete object (Cat)

This is also from Java source. The Cat class has a cat voice and It has a bark behavior.

Cat.java


package animal;

public class Cat extends Animal {
    private String voice = "Nya Nya";

    public void bark() {
        System.out.println(this.voice);
    }
}

Next is Go language.

Since the Go language has no concept of class, ** define a cat with a struct ** and declare it as a cat type data type (type). However, data types cannot have values ("voice = Nya Nya") like Java classes do.

Here comes the ** Java constructor-like method NewCat () **. This method gives a value ("Nya Nya") when creating an instance of data type (Cat).

Then implement Bark () declared in the interface defined in Animal.go. The point here is the receiver. ** A receiver is a "method defined for a data type" **. Here it is a method of data type (type Cat struct) (Bark ()).

Cat.go


package animal

import (
	"fmt"
)

type Cat struct {    //Cat structure(struct)Defined in
	voice string
}

func NewCat() *Cat {  //Java constructor-like method
	return &Cat{"Nya Nya"} 
}

func (c Cat) Bark(){  //receiver
	fmt.Println(c.voice)
}

5-3. Concrete object (Dog)

Omitted because it is the same as Cat.

5-4. Main object

Finally, Main. This is also from Java. Create an instance of a concrete object of Cat, Dog, You are calling the method (bark) defined in the interface (Animal).

Main.java


package main;

import animal.Cat;
import animal.Dog;
import animal.Animal;

public class Main {
    public static void main(String[] args) {
       Animal c = new Cat();
       Animal d = new Dog();

       c.bark();
       d.bark();
    }
}

Next is Go language. Converting a concrete object (Cat or Dog) to an interface (Baker) is basically the same as Java, so it's easy to understand.

Main.go


package main

import (
	"animal"
)

func main(){
	var c animal.Barker = animal.NewCat()
	var d animal.Barker = animal.NewDog()

	c.Bark()
	d.Bark()
}

Summary

By implementing the same design in Go language and Java language, it seems that Java language design (class design) can be applied to Go language. However, since there are differences in language specifications when implementing, it seems that it is necessary to remember the implementation pattern like this time in order to apply the Java language class design to Go language.

The Java language may be suitable for implementation in large numbers (large scale) because it allows you to constrain between classes by inheritance and implementations and clarify the role of each. .. On the other hand, I felt that the Go language is suitable for applications that are frequently modified because the restrictions are loose and the dependency between objects is low.

Reference URL

Recommended Posts

Java programmer touched Go language (Implement Java inheritance in Go language)
Implement recursive closures in Go
Hello World in GO language
Implement Table Driven Test in Java
Try implementing Yubaba in Go language
Use optinal type-like in Go language
Post to slack in Go language
Write tests in GO language + gin
Do something object-oriented in GO language
Java programmer tried to touch Go language (for the time being)
Implement and understand union-find trees in Go
I wrote the hexagonal architecture in go language
Create a web server in Go language (net/http) (2)
Create a web server in Go language (net / http) (1)
Go language to see and remember Part 7 C language in GO language