JV
GO

Java to Go

10 lessons

Progress0%
1Variables & Types2Classes → Structs3Interfaces4Exception Handling → Error Values5Threads → Goroutines6Slices and Collections7Packages and Modules8Testing9Go Standard Library10Context and Cancellation
All Mirror Courses
JV
GO
Interfaces
MirrorLesson 3 of 10
Lesson 3

Interfaces

Structural vs nominal typing

Introduction

In this lesson, you'll learn about interfaces in Go. Coming from Java, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.

Mirror Card
JV
From Java:

In Java, you're familiar with structural vs nominal typing.

GO
In Go:

Go has its own approach to structural vs nominal typing, which we'll explore step by step.

The Go Way

Let's see how Go handles this concept. Here's a typical example:

GO
Go Example
package main

// Structural typing — no "implements" needed
type Stringer interface {
    String() string
}

type Dog struct{ Name string }

// Dog satisfies Stringer automatically
func (d Dog) String() string { return "Dog: " + d.Name }

type Cat struct{ Name string }
func (c Cat) String() string { return "Cat: " + c.Name }

func print(s Stringer) {
    fmt.Println(s.String())
}

print(Dog{"Rex"})    // works
print(Cat{"Whiskers"}) // works too

Comparing to Java

Here's how you might have written similar code in Java:

JV
Java (What you know)
// Nominal typing — must declare "implements"
public interface Stringer {
    String toString();
}

public class Dog implements Stringer { // explicit
    private String name;
    public Dog(String name) { this.name = name; }
    @Override
    public String toString() { return "Dog: " + name; }
}

// Must implement all interface methods
// Or get a compile error
Mirror Card
JV
From Java:

You may be used to different syntax or behavior.

GO
In Go:

Go uses structural typing — implement the methods, satisfy the interface

Mirror Card
JV
From Java:

You may be used to different syntax or behavior.

GO
In Go:

Java requires explicit 'implements'; Go needs no declaration

Mirror Card
JV
From Java:

You may be used to different syntax or behavior.

GO
In Go:

Go interfaces are typically small (1-2 methods); Java can have many

Mirror Card
JV
From Java:

You may be used to different syntax or behavior.

GO
In Go:

Go empty interface{} (or any) accepts any type; Java uses Object

Step-by-Step Breakdown

1. Structural vs Nominal

Java requires explicit 'implements Interface'. Go just checks if the type has the required methods — no declaration needed.

JV
Java
public class Dog implements Stringer { ... }
GO
Go
type Dog struct{ Name string }
func (d Dog) String() string { return "Dog: " + d.Name }
// Dog automatically satisfies Stringer

2. Small Interfaces

Go interfaces are often just 1 method — io.Reader has Read(), fmt.Stringer has String(). Java interfaces tend to be larger.

GO
Go
type Reader interface { Read(p []byte) (n int, err error) }
type Writer interface { Write(p []byte) (n int, err error) }
type ReadWriter interface { Reader; Writer }  // composed

3. Empty Interface

Go's empty interface (interface{} or any) accepts any value — like Java's Object. Use with caution.

JV
Java
void process(Object obj) { ... }
GO
Go
func process(obj any) { ... }  // any = interface{}

Common Mistakes

When coming from Java, developers often make these mistakes:

  • Go uses structural typing — implement the methods, satisfy the interface
  • Java requires explicit 'implements'; Go needs no declaration
  • Go interfaces are typically small (1-2 methods); Java can have many
Common Pitfall
Don't assume Go works exactly like Java. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • Go structural typing: no 'implements' declaration
  • Go interfaces are typically tiny (1-2 methods)
  • interface{} / any = Java Object
  • Composed interfaces via embedding
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your Java code in Go to practice these concepts.
PreviousNext