Step Over to Go Part 7 of 50
  1. 1 Goodbye .csproj, Hello go.mod
  2. 2 The Phrasebook: C# Concepts in Go Terms
  3. 3 You Know .NET, Now Meet the Go Toolchain
  4. 4 No Enums? No Problem (Sort Of)
  5. 5 Generics: Late to the Party
  6. 6 The Million Dollar Mistake, Differently
  7. 7 Your Class Is Now a Struct (And That's Fine)
  8. 8 Where Did My Properties Go?
  9. 9 if err != nil: Your New Reality
  10. 10 Panic and Recover: The Emergency Exit
  11. 11 The switch Statement You Always Wanted
  12. 12 Stack vs Heap: Go's Escape Analysis
  13. 13 Pointers Without the Pain
  14. 14 Slices Are Not Arrays (And Neither Is List<T>)
  15. 15 Channels: The sync You Didn't Know You Wanted
  16. 16 Context Is King
  17. 17 Async/Await vs Goroutines: A Mindset Shift
  18. 18 select: Multiplexing Like a Pro
  19. 19 Mutexes and WaitGroups: When Channels Aren't the Answer
  20. 20 The Empty Interface and Type Assertions
  21. 21 Composition Over Inheritance (For Real This Time)
  22. 22 Implicit Interfaces: Scary Then Brilliant
  23. 23 Benchmarks and Profiling Out of the Box
  24. 24 Mocking in a Language Without Mockito
  25. 25 Multi-Module Repos: Monorepo Thinking
  26. 26 Organising a Real Project
  27. 27 Testing Without a Framework
  28. 28 Configuration Without IOptions<T>
  29. 29 Database Access: database/sql vs Entity Framework
  30. 30 Debugging: Delve vs Visual Studio's Comfort
  31. 31 Generate All the Things
  32. 32 GORM and Friends: When You Do Want an ORM
  33. 33 HTTP Services: net/http vs ASP.NET Core
  34. 34 JSON: Struct Tags and the Marshal Dance
  35. 35 Linting and Formatting: gofmt Is Non-Negotiable
  36. 36 Logging: slog and the Structured Logging Story
  37. 37 Shrinking Binaries and Build Tags
  38. 38 Dockerfile for Go: Simpler Than You'd Think
  39. 39 GitHub Actions for Go Projects
  40. 40 Health Checks and Readiness Probes
  41. 41 Kubernetes and Go: A Natural Fit
  42. 42 AWS Lambda and Go: Cold Starts That Don't Hurt
  43. 43 Metrics, Traces, and Logs: The OpenTelemetry Way
  44. 44 Single Binary Deploys: The Killer Feature
  45. 45 Versioning Your Modules
  46. 46 What I Actually Miss From C#
  47. 47 What I'll Never Go Back For
  48. 48 Six Months In: Was It Worth It?
  49. 49 Forty-Eight Posts on Moving From C# to Go, in the Right Order
  50. 50 Range-Over-Func Iterators for People Who Miss LINQ
About this series

Your Class Is Now a Struct (And That's Fine)

Here's something that'll feel wrong for about a week: Go doesn't have classes. Not “Go has classes but calls them something else”. It really doesn't have them. No inheritance. No class hierarchies. No virtual or override. No protected.

If you've spent years designing C# class hierarchies, this feels like having a limb removed. Then you start writing Go code and realise… you don't actually miss it that much.

Let's dig into why.

The Basic Translation

In C#, you'd write:

public class User
{
    public string Name { get; set; }
    public string Email { get; set; }
    public DateTime CreatedAt { get; set; }
    
    public User(string name, string email)
    {
        Name = name;
        Email = email;
        CreatedAt = DateTime.UtcNow;
    }
    
    public string DisplayName() => $"{Name} <{Email}>";
}

In Go:

type User struct {
    Name      string
    Email     string
    CreatedAt time.Time
}

func NewUser(name, email string) User {
    return User{
        Name:      name,
        Email:     email,
        CreatedAt: time.Now().UTC(),
    }
}

func (u User) DisplayName() string {
    return fmt.Sprintf("%s <%s>", u.Name, u.Email)
}

A few things to notice:

  1. No constructor syntax. NewUser is just a function. By convention, Go uses NewX functions, but there's nothing special about them.
  2. Methods are declared separately. That func (u User) syntax attaches a method to the type.
  3. No access modifiers. Capitalisation controls visibility (covered in the next post).
  4. Fields are just fields. No properties, no getters/setters.

Value Semantics by Default

Here's the big one. In C#, classes are reference types. When you pass a User to a method, you're passing a reference. Modifications affect the original.

void UpdateEmail(User user)
{
    user.Email = "new@example.com";  // modifies the original
}

In Go, structs are value types by default. Pass a struct, you're passing a copy.

func UpdateEmail(u User) {
    u.Email = "new@example.com"  // modifies the copy, original unchanged
}

If you want reference semantics, you use pointers explicitly:

func UpdateEmail(u *User) {
    u.Email = "new@example.com"  // modifies the original via pointer
}

This explicitness is jarring at first. In C#, the type itself determines whether you get value or reference semantics (struct vs class). In Go, the calling convention determines it. Same type, different behaviour depending on whether you use User or *User.

When to Use Pointers

The rule of thumb:

Scenario Use
Method modifies the receiver *User (pointer receiver)
Struct is large *User (avoid copy overhead)
You need nil to mean “absent” *User (pointers can be nil)
Small, immutable data User (value)
You want copy-on-pass safety User (value)

In practice, most methods use pointer receivers. It's more consistent, and you don't have to think about whether each method modifies state.

// Pointer receiver - conventional for most methods
func (u *User) UpdateEmail(email string) {
    u.Email = email
}

// Value receiver - acceptable for pure getters on small structs
func (u User) DisplayName() string {
    return fmt.Sprintf("%s <%s>", u.Name, u.Email)
}

Composition Over Inheritance

Go doesn't have inheritance. Full stop. No class Admin : User. No protected members. No virtual methods.

Instead, Go has embedding, a form of composition that feels almost like inheritance but isn't.

type Person struct {
    Name string
    Age  int
}

func (p Person) Greet() string {
    return fmt.Sprintf("Hello, I'm %s", p.Name)
}

type Employee struct {
    Person          // embedded - not a field name, just the type
    EmployeeID string
    Department string
}

Now Employee has all of Person's fields and methods:

emp := Employee{
    Person:     Person{Name: "Alice", Age: 30},
    EmployeeID: "E123",
    Department: "Engineering",
}

fmt.Println(emp.Name)    // "Alice" - promoted from Person
fmt.Println(emp.Greet()) // "Hello, I'm Alice" - method promoted too

This looks like inheritance, but it's not:

  • No polymorphism. You can't pass an Employee where a Person is expected.
  • No override. If Employee defines its own Greet(), it shadows Person.Greet().
  • The embedded type is still accessible. emp.Person.Name works.

When You'll Miss Inheritance

Let's be honest: sometimes you'll miss it.

Template Method Pattern: In C#, you'd have a base class with a virtual method that subclasses override. Go has no direct equivalent. You use interfaces and composition instead, which works but feels more verbose.

Protected Members: There's no “visible to derived types” concept. Everything is either exported (public) or unexported (package-private). For library design, this can be limiting.

Deep Hierarchies: If your C# design has Animal -> Mammal -> Dog -> Labrador, Go will make you rethink that structure entirely. Usually for the better, but the rethinking takes effort.

Interfaces Are Implicit

This is where Go gets properly interesting. Interfaces are satisfied implicitly. No implements keyword.

In C#:

public interface IGreeter
{
    string Greet();
}

public class Person : IGreeter  // explicit implementation
{
    public string Name { get; set; }
    public string Greet() => $"Hello, I'm {Name}";
}

In Go:

type Greeter interface {
    Greet() string
}

type Person struct {
    Name string
}

func (p Person) Greet() string {
    return fmt.Sprintf("Hello, I'm %s", p.Name)
}

// Person now implements Greeter - no declaration needed

If a type has the methods an interface requires, it implements that interface. Period. No ceremony.

This has profound implications:

  1. You can define interfaces after the fact. Create an interface that existing types already satisfy.
  2. Small interfaces are encouraged. The standard library is full of one-method interfaces.
  3. Testing is trivial. Any type with the right methods can be substituted.

The io.Reader interface is the canonical example:

type Reader interface {
    Read(p []byte) (n int, err error)
}

One method. But files, network connections, byte buffers, HTTP response bodies: they all implement it. Polymorphism without inheritance.

The Comparison

Aspect C# Classes Go Structs
Reference/value Reference by default Value by default
Inheritance Full class inheritance Embedding (composition)
Interface implementation Explicit (implements) Implicit (duck typing)
Constructors Special syntax Convention (NewX functions)
Access modifiers public, private, protected, internal Exported (capital) or unexported
Polymorphism Inheritance + interfaces Interfaces only
Method declaration Inside class body Separate, attached to type

What I Actually Miss

After a few months, here's my honest assessment:

Don't miss at all:

  • Complex inheritance hierarchies (they were usually a mistake anyway)
  • The virtual/override dance
  • Explicit interface implementation for conflicts

Sometimes miss:

  • protected for carefully designed extension points
  • Abstract classes with partial implementations
  • The template method pattern (can be done in Go, just clunkier)

Actively prefer Go's approach:

  • Implicit interface satisfaction
  • Explicit value vs reference choice at call site
  • Composition via embedding
  • Small, focused interfaces

The shift from “design your class hierarchy upfront” to “compose small pieces and let interfaces emerge” takes adjustment. But it tends to produce simpler code.

The Practical Bit

If you're converting C# code to Go, here's the process I use:

  1. Start with structs for data. No methods yet, just fields.
  2. Add methods as needed. Use pointer receivers by default.
  3. Extract interfaces late. When you need polymorphism, create the smallest interface that works.
  4. Embed for code reuse. Not “inheritance”, just “I want those fields and methods too”.
  5. Resist the urge to build hierarchies. If you're thinking “base class”, stop and reconsider.

The code ends up flatter. More types, shallower relationships. It's different, not worse.


Next up: visibility and the death of properties. Why Go uses capitalisation instead of access modifiers, and why you'll stop missing getters and setters faster than you'd expect.