better way to express a function in Go (Struct methods) - Stack Overflow
How to organize struct with lots of methods
Calling the embedding struct's overloaded method from the embedded struct - Technical Discussion - Go Forum
"Inheritance" of types in Golang
Videos
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 :(
In Go is there a recommended/idiomatic way to logically organize/break up a massive struct with 1000+ lines of methods into separate files?
I have a Server struct for a gRPC server that has a ton of different methods for things like:
- interacting with Redis
- gRPC request handlers running a distributed election algorithm to select a leader/coordinator for a group of nodes
- handling data replication
Many of these methods are are server side gRPC request handlers, while others create gRPC clients to interact with other server nodes.
I am wondering if there is some recommended way to break these methods up into logical groups. Can I put the election methods in one file, replication methods in another, all of them with the same (s* Server) receiver?