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
Classes → Structs
MirrorLesson 2 of 10
Lesson 2

Classes → Structs

Defining types and behavior

Introduction

In this lesson, you'll learn about classes → structs 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 defining types and behavior.

GO
In Go:

Go has its own approach to defining types and behavior, 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

type Person struct {
    Name string
    Age  int
}

// No constructor — struct literal
// Methods via receivers
func (p Person) Greet() string {
    return "Hi, I'm " + p.Name
}

func (p *Person) Birthday() {
    p.Age++ // pointer receiver for mutation
}

p := Person{Name: "Alice", Age: 30}
fmt.Println(p.Greet())
p.Birthday()

Comparing to Java

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

JV
Java (What you know)
public class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() { return name; }

    public String greet() {
        return "Hi, I'm " + name;
    }

    public void birthday() { age++; }
}

Person p = new Person("Alice", 30);
System.out.println(p.greet());
Mirror Card
JV
From Java:

You may be used to different syntax or behavior.

GO
In Go:

Go structs have no constructor — use struct literals

Mirror Card
JV
From Java:

You may be used to different syntax or behavior.

GO
In Go:

Go methods are defined outside the struct with a receiver

Mirror Card
JV
From Java:

You may be used to different syntax or behavior.

GO
In Go:

Go has no private — unexported (lowercase) is package-private

Mirror Card
JV
From Java:

You may be used to different syntax or behavior.

GO
In Go:

Go pointer receiver (*T) for mutation; Java fields mutable by default

Step-by-Step Breakdown

1. No Constructor

Go doesn't have constructors. Initialize with struct literals. Use a New() function by convention for complex initialization.

JV
Java
new Person("Alice", 30)
GO
Go
Person{Name: "Alice", Age: 30}
// or by convention: NewPerson("Alice", 30)

2. Receiver Methods

Go methods are defined outside the struct with a receiver — like Java methods but explicit about the receiver.

JV
Java
public String greet() { return "Hi, " + name; }
GO
Go
func (p Person) Greet() string { return "Hi, " + p.Name }

3. Capitalization = Access Control

Go uses capitalization for visibility: Name (capital) is exported (public); name (lowercase) is package-private.

JV
Java
private String name; public String getName()
GO
Go
Name string  // exported
name string  // unexported (package-private)

Common Mistakes

When coming from Java, developers often make these mistakes:

  • Go structs have no constructor — use struct literals
  • Go methods are defined outside the struct with a receiver
  • Go has no private — unexported (lowercase) is package-private
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

  • No constructors — struct literals or New() function
  • Methods use receiver syntax outside the struct
  • Capital = exported; lowercase = package-private
  • Pointer receiver needed for mutation
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