Think of it like this. The parameter before the function name can just become the first parameter of the function, so if you have a struct A then func (a *A) f1() {} is the same as func f2(a *A) {} That's all that's going on there. When you call &A{}.f1() it's the same as doing f2(&A{}). What this lets you do later is pass in A to interfaces that define an f1 function. type I interface { f1() } func wantsI(i I) { i.f1() } a := &A{} wantsI(a) This doesn't look all that impressive until you realize you can put I in another package, and that package doesn't have to depend on anything that knows A at all. Answer from i_should_be_coding on reddit.com
🌐
Go by Example
gobyexample.com › methods
Go by Example: Methods
Go supports methods defined on struct types · This area method has a receiver type of *rect
🌐
Reddit
reddit.com › r/golang › am i understanding go structs methods well?
r/golang on Reddit: Am I Understanding Go Structs Methods Well?
February 15, 2024 -

Hello everybody, it's me once again with a newbie question. Sorry for that, hehe.So, I was doing this course and saw that you can associate methods with structs. Or at least, that's what I think is happening here.Please, let's look at this code:

type EmployeeContract struct {
EmpID         int
Salary        float64
TaxPercentage float64

}

type FreelancerContract struct { EmpID int HourlyRate float64 HoursWorked int }

// 'EmployeeContract' Salary is the base salary - calculated taxes func (e EmployeeContract) CalculateSalary() float64 { return e.Salary - (e.TaxPercentage * e.Salary) }

// 'FreelancerContract' salary is the hourly rate * hours worked -- they declare taxes themselves func (f FreelancerContract) CalculateSalary() float64 { return f.HourlyRate * float64(f.HoursWorked) }

type SalaryCalculator interface { CalculateSalary() float64 }

At the beginning, I didn't understand what was going on. Why is there a parameter before the name of the method in CalculateSalary()? But then I realized it was not a parameter. It was more like an association to the struct FreelancerContract. (Am I getting this right?)

Go is kinda hard for me, even though I already know how to code and stuff, because its syntax is not really something I'm used to. A few days ago, I barely understood pointers, but now I understand them, thanks to you guys.

What I like to do is to "compare" the Go code to Java, Python, or the languages I'm used to.

Please tell me if this is something I should do or if it's making my learning worse.

But this is my interpretation:FreelancerContract is like a Java or C# class, and CalculateSalary() is one of its methods. So in Java, for example, it may look like this:

public class EmployeeContract {
private int empID;
private double salary;
private double taxPercentage;

public EmployeeContract(int empID, double salary, double taxPercentage) {
    this.empID = empID;
    this.salary = salary;
    this.taxPercentage = taxPercentage;
}

public double calculateSalary() {
    return salary - (taxPercentage * salary);
}

}

public class FreelancerContract { private int empID; private double hourlyRate; private int hoursWorked;

public FreelancerContract(int empID, double hourlyRate, int hoursWorked) {
    this.empID = empID;
    this.hourlyRate = hourlyRate;
    this.hoursWorked = hoursWorked;
}

public double calculateSalary() {
    return hourlyRate * hoursWorked;
}

}

In Go by Example, they define this type of methods like this:

Methods can be defined for either pointer or value receiver types.Go automatically handles conversion between values and pointers for method calls. You may want to use a pointer receiver type to avoid copying on method calls or to allow the method to mutate the receiving struct.

Please correct me if i'm wrong or if i'm missing something. I still don't get it 100%

I'm sorry that the code doesn't appear formatted, for some reason, even if I edit it, Reddit places it like this. Maybe I should have written it in markdown :(

Top answer
1 of 2
4
Think of it like this. The parameter before the function name can just become the first parameter of the function, so if you have a struct A then func (a *A) f1() {} is the same as func f2(a *A) {} That's all that's going on there. When you call &A{}.f1() it's the same as doing f2(&A{}). What this lets you do later is pass in A to interfaces that define an f1 function. type I interface { f1() } func wantsI(i I) { i.f1() } a := &A{} wantsI(a) This doesn't look all that impressive until you realize you can put I in another package, and that package doesn't have to depend on anything that knows A at all.
2 of 2
1
Why is there a parameter before the name of the method in CalculateSalary()? But then I realized it was not a parameter. It totally is a parameter - just a special one. It's called the method receiver. If you're familiar with other OO languages, methods have a special variable in scope: this or self. This is a hidden parameter that refers to the specific object the method is operating on. That "parameter" before the method name, that's Go's version of this, only it's explicit. You get to choose whether it's value or pointer semantics, and get to choose the name. Here's some code to illustrate: c := EmployeeContract{} // You can call a method right from a type EmployeeContract.CalculateSalary(c) // Or a variable c.CalculateSalary() In fact, look at EmployeeContract.CalculateSalary: you specified no parameters, but it expects one: the instance of the EmployeeContract. That's because when you call c.CalculateSalary(), under the hood it becomes EmployeeContract.CalculateSalary(c).
Discussions

better way to express a function in Go (Struct methods) - Stack Overflow
This is a function to traverse an array of Points. A Point is a struct with elements x and y of type int. The traverse function is called from another function with a value of dir. The if check at ... More on stackoverflow.com
🌐 stackoverflow.com
"Inheritance" of types in Golang
Golang does not have inheritance. More on reddit.com
🌐 r/golang
19
0
July 29, 2024
vscode: Show all methods of a struct?
In the Explorer on the left there's an "Outline" section. You can configure it in its "..." menu to follow the cursor. More on reddit.com
🌐 r/golang
6
6
February 13, 2024
Pointer receiver or value receiver?
My (likely unpopular) opinion: a pointer receiver is the lowest common denominator. It won't get allocated on the heap, and it will serve you regardless of whether you pass around your struct value by copying it, or via a pointer to it. Yeah, others are right about immutability and stuff, but believe me, for a large majority of use cases, it won't matter. TL;DR: pointer receiver More on reddit.com
🌐 r/golang
26
8
January 19, 2024
🌐
DEV Community
dev.to › jpoly1219 › structs-methods-and-receivers-in-go-5g4f
Structs, Methods, and Receivers in Go - DEV Community
January 30, 2023 - Methods are like functions, but they are bound to a certain type or object, which are called receivers. I honestly don't know why they are called methods, and it's probably because engineers are bad at naming things. Receivers do NOT have to be structs.
🌐
Medium
medium.com › @danielabatibabatunde1 › simplifying-structs-methods-and-interfaces-in-golang-e86a0c4618aa
Simplifying Structs, Methods and Interfaces in Golang. | by Abati Babatunde Daniel | Medium
March 7, 2025 - Golang has the ability to address most OOP concepts by implementing Structs which are used to define the data structures of your project and model real-world entity — similar to the way classes work. Go defining methods for which they are simply functions relating to structs to Interfaces which defines a set of method signatures (basically means any type that has these specific methods belongs to me) — polymorphism.
🌐
GeeksforGeeks
geeksforgeeks.org › go language › go-structs-methods-and-receivers
Go - Structs, Methods, and Receivers - GeeksforGeeks
July 23, 2025 - Both structs in Go and classes in other OOP languages group related data (fields) together. Both have the ability to define methods for their behavior.
🌐
Go
go.dev › tour › methods
A Tour of Go: Methods
More types: structs, slices, and maps.
🌐
CodeSignal
codesignal.com › learn › courses › go-structs-basics-revision › lessons › go-structs-and-methods-an-introduction
Go Structs and Methods: An Introduction
Accessing struct fields involves the dot (.) operator, and initialization occurs using composite literals or individual assignments. ... Differently from other OOP languages, in Go methods are simply functions with a receiver argument. Receiver functions in Go feature the special receiver ...
Find elsewhere
🌐
Medium
medium.com › @kamruljpi › structs-and-methods-in-go-part-8-04f89008435a
Structs and Methods in Go(Part-8) | by Kamruzzaman Kamrul | Medium
December 5, 2023 - Structs are a composite data type in Go that groups together variables (fields) under a single name. Methods, on the other hand, are functions associated with a specific type.
🌐
GeeksforGeeks
geeksforgeeks.org › go language › how-to-add-a-method-to-struct-type-in-golang
How to add a method to struct type in Golang? - GeeksforGeeks
July 15, 2025 - There will be no difference in calling the method whether the instance you are using to call it with is a pointer or a value, Go will automatically do the conversion for you. ... package main import "fmt" // taking a struct type Rect struct { len, wid int } func (re Rect) Area_by_value() int { return re.len * re.wid } func (re *Rect) Area_by_reference() int { return re.len * re.wid } // main function func main() { r := Rect{10, 12} fmt.Println("Length and Width is:", r) fmt.Println("Area of Rectangle is:", r.Area_by_value()) fmt.Println("Area of Rectangle is:", r.Area_by_reference()) fmt.Println("Area of Rectangle is:", (&r).Area_by_value()) fmt.Println("Area of Rectangle is:", (&r).Area_by_reference()) } Output:
🌐
Learn Go with Tests
quii.gitbook.io › learn-go-with-tests › go-fundamentals › structs-methods-and-interfaces
Structs, methods & interfaces - Learn Go with Tests - GitBook
February 26, 2026 - Declaring structs to create your own data types which lets you bundle related data together and make the intent of your code clearer · Declaring interfaces so you can define functions that can be used by different types (ad hoc polymorphism) Adding methods so you can add functionality to your data types and so you can implement interfaces
🌐
Relia Software
reliasoftware.com › blog › golang-struct
The Ultimate Guide to Golang Structs with Code Example | Relia Software
April 23, 2025 - In Go, you can define methods on structs to associate specific functions with a given struct type. Methods are similar to regular functions but have a receiver argument that specifies the struct type they are associated with.
🌐
LabEx
labex.io › tutorials › go-how-to-implement-methods-on-structs-437941
How to implement methods on structs - Golang
In Golang, methods are functions associated with a specific type, particularly structs.
🌐
Go Resources
golang-book.com › books › intro › 9
Structs and Interfaces — An Introduction to Programming in Go | Go Resources
Like a struct an interface is created using the type keyword, followed by a name and the keyword interface. But instead of defining fields, we define a “method set”. A method set is a list of methods that a type must have in order to “implement” the interface.
🌐
Medium
medium.com › rungo › anatomy-of-methods-in-go-f552aaa8ac4a
Anatomy of methods in Go. Since there is no class-object… | by Uday Hiwarale | RunGo | Medium
September 1, 2020 - GOLANG Anatomy of methods in Go Go does not support the Object-Oriented paradigm but structure resembles the class architecture. To add methods to a structure, we need to use functions with a …
🌐
TutorialsPoint
tutorialspoint.com › how-to-add-a-method-to-struct-type-in-golang
How to add a method to struct type in Golang?
April 20, 2023 - ... In the above example, we define a Person struct that has two fields, Name and Age. Adding a method to a struct in Golang is simple. We define the method outside of the struct definition and use the struct as the receiver for the method.
🌐
Bogotobogo
bogotobogo.com › GoLang › GoLang_Structs.php
GoLang Tutorial - Structs and receiver methods - 2020
Note that we can not only access a specific field of a struct but also we can modify the value of any field. Go does not have classes. However, we can define ... It's a Go way of creating a method for a struct!
🌐
Go by Example
gobyexample.com › structs
Go by Example: Structs
Go’s structs are typed collections of fields. They’re useful for grouping data together to form records · This person struct type has name and age fields
🌐
Cristian Curteanu
cristiancurteanu.com › understanding-the-basics-a-beginners-guide-to-golang-structs
Understanding the Basics: A Beginner's Guide to Golang Structs
December 1, 2023 - While structs in Go primarily hold data, you can also associate functions with them, known as "golang struct methods." These methods allow you to define behaviors on your structs, much like methods in object-oriented programming languages.
🌐
Go
go.dev › tour › methods › 3
Methods continued
More types: structs, slices, and maps. ... Congratulations! ... Congratulations! ... Congratulations! ... Where to Go from here... You can declare a method on non-struct types, too.