C
GO

C to Go

10 lessons

Progress0%
1Variables & Types2Functions3Arrays & Slices4Structs & Methods5Pointers6Concurrency7Header Files → Packages8Error Handling9Testing10Standard Library
All Mirror Courses
C
GO
Structs & Methods
MirrorLesson 4 of 10
Lesson 4

Structs & Methods

Defining data types and associated behavior

Introduction

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

Mirror Card
C
From C:

In C, you're familiar with defining data types and associated behavior.

GO
In Go:

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

import "fmt"

type Person struct {
    Name string
    Age  int
}

// Value receiver — read-only
func (p Person) Greet() string {
    return "Hi, I'm " + p.Name
}

// Pointer receiver — mutation
func (p *Person) Birthday() {
    p.Age++
}

func main() {
    p := Person{Name: "Alice", Age: 30}
    fmt.Println(p.Greet())
    p.Birthday()
    fmt.Printf("Now %d\n", p.Age)
}

Comparing to C

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

C
C (What you know)
#include <stdio.h>
#include <string.h>

typedef struct {
    char name[50];
    int age;
} Person;

/* Functions on structs */
void person_greet(const Person *p) {
    printf("Hi, I'm %s\n", p->name);
}

void person_birthday(Person *p) {
    p->age++;
}

int main() {
    Person p;
    strncpy(p.name, "Alice", sizeof(p.name));
    p.age = 30;
    person_greet(&p);
    person_birthday(&p);
    printf("Now %d\n", p.age);
}
Mirror Card
C
From C:

You may be used to different syntax or behavior.

GO
In Go:

Go methods attach to struct via receiver syntax; C uses separate functions with pointer arg

Mirror Card
C
From C:

You may be used to different syntax or behavior.

GO
In Go:

Go value receiver (p Person) = C's const Person*; pointer receiver (p *Person) = Person*

Mirror Card
C
From C:

You may be used to different syntax or behavior.

GO
In Go:

Go uses p.Method() not p->method(); Go auto-dereferences pointers

Mirror Card
C
From C:

You may be used to different syntax or behavior.

GO
In Go:

Go struct literals use field names; C requires member-by-member or designated initializers

Step-by-Step Breakdown

1. Receiver Methods

Go attaches functions to a type using a receiver, similar to C's pattern of passing a struct pointer as the first argument.

C
C
void person_greet(const Person *p) { ... }
GO
Go
func (p Person) Greet() string { ... }

2. Pointer vs Value Receiver

Use a pointer receiver (*T) when the method must modify the struct — equivalent to C's non-const pointer parameter.

C
C
void person_birthday(Person *p) { p->age++; }
GO
Go
func (p *Person) Birthday() { p.Age++ }

3. Struct Literals

Go struct literals are cleaner than C's member-by-member initialization.

C
C
Person p; strncpy(p.name, "Alice", 50); p.age = 30;
GO
Go
p := Person{Name: "Alice", Age: 30}

Common Mistakes

When coming from C, developers often make these mistakes:

  • Go methods attach to struct via receiver syntax; C uses separate functions with pointer arg
  • Go value receiver (p Person) = C's const Person*; pointer receiver (p *Person) = Person*
  • Go uses p.Method() not p->method(); Go auto-dereferences pointers
Common Pitfall
Don't assume Go works exactly like C. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • Go receiver methods replace C's struct-pointer function pattern
  • Value receiver = const pointer; pointer receiver = mutable pointer
  • Go auto-dereferences — use p.Field not p->Field
  • Struct literals are concise in Go
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your C code in Go to practice these concepts.
PreviousNext