1. Basic Definitions

🧱 Interface

type employee interface {
	getName() string
	getSalary() int
}
  • Keyword: interface

  • What it is: A behavior contract — any type that has both getName() and getSalary() methods automatically “implements” the employee interface.

  • In plain English: “Anything that can tell me its name and salary is an employee.”


🧱 Structs

type contractor struct {
	name         string
	hourlyPay    int
	hoursPerYear int
}
type fullTime struct {
	name   string
	salary int
}
  • Keyword: struct

  • What they are: Data containers (like Python classes with just attributes, no logic).

    • contractor has fields: name, hourlyPay, hoursPerYear

    • fullTime has fields: name, salary

  • Purpose: Store the actual data for each type of employee.


⚙️ Methods

  • Keyword: func (receiver TypeName)

  • What they are: Functions that are attached to a specific type.

    • (c contractor) → receiver: the function belongs to the contractor type.

    • (ft fullTime) → receiver: the function belongs to the fullTime type.

  • Why: These make both contractor and fullTime satisfy the employee interface because they both define getName() and getSalary().

Receiver terminology:

Example
Meaning

(c contractor)

A value receiver (works on a copy)

(ft *fullTime)

A pointer receiver (would work on the original)


⚙️ Functions (general)

Every method in Go is technically a function, but functions without receivers are standalone. You don’t have one in this snippet, but an example would be:

That’s a function, not a method, because it isn’t attached to a specific type.


🧠 Summary Table

Concept
Keyword / Example
Purpose

Interface

type employee interface { ... }

Defines required behaviors (method signatures)

Struct

type contractor struct { ... }

Holds data fields (like a class with attributes)

Method

func (c contractor) getSalary() int

Function attached to a struct (defines behavior)

Receiver

(c contractor) or (ft fullTime)

The variable the method operates on (like self in Python)

Function

func add(x, y int) int

A regular standalone function (not tied to a type)


🧩 In Plain English

Go thing
Python equivalent
Example

struct

Class with only attributes

class Contractor: with __init__

method

Function with self parameter

def get_salary(self):

receiver

self in methods

(c contractor)self

interface

Abstract base class or duck typing

class Employee(ABC): def get_salary(self): ...

function

Regular def outside class

def add(x, y): return x + y


🧩 How It All Connects

✅ Both contractor and fullTime implement the employee interface because they each have:

  • getName() string

  • getSalary() int

So this works:

✅ Output:


🧩 All methods are functions — but not all functions are methods

  • A function is just a standalone block of code you can call.

  • A method is a function that’s attached to a specific type (like a struct).

So, methods are really just functions with a receiver.


1

Receiver = like self in Python

In Python:

In Go:

Language
Keyword
Meaning

Go

(u User)

The receiver (like self)

Python

self

The instance the method is called on

2

Two kinds of receivers

Value receiver (copy)

  • Works on a copy of the struct.

  • Doesn’t modify the original.

Pointer receiver (reference)

  • Works on the original struct.

  • Can modify the real data.

3

Example: Function vs. Method

Function (no receiver)

Call:

Method (has a receiver)

Call:

4

Summary

Concept
Example
Description

Function

func add(x, y int) int

Standalone code block

Method

func (u User) greet() string

Function bound to a type

Receiver

(u User)

The instance the method is called on

Pointer Receiver

(u *User)

Lets you modify the original value

Value Receiver

(u User)

Works on a copy


💡 Think of it like this

“Every method is a function that happens to live on a specific type.”

It’s the receiver that makes it a method.