I’m coming from C# so i’m a bit confused. Your userRepository struct only has the db variable and yes you are implementing the same functions in the interface{} but i never understood (learned go many times 😂) how by just emulating the interface functions there is a direct connection with the interface itself and not the db variable. I know not to think oop but it is still confusing
Hi Jay I can understand your situation. I also built a lot of applications in C# in the past so we can not compare Object-Oriented (C#) with Procedural (Go) programming. Go does have the class concept and it does not work the way C# interfaces do. In Go you don't need to explicitly declare that a type implements an interface. Instead of if a type provides definitions for all the methods declared in an interface, Go complier will automatically consider that type as implementing the interface. Let's try to understand what's going on here: Forget about the C# concept Go is a totally different language :D We have our Interface that needs to be implemented but how we will do that? In this case, we need to implement the interface functions somehow so let's assume we can remove this part ("r userRepository") from the below function. Now it looks good and we have implemented the interface but we have an issue here. Without type how we can pass the db connection so that the interface can perform the DB Operations as described in the interface? // a function without type func CreateBankAccount(e domain.BankAccount) error { } That's why we are creating a repository struct to use as a receiver function to implement the interface like below: type userRepository struct { db *gorm.DB } // function with type (userRepository struct) func (r userRepository) CreateBankAccount(e domain.BankAccount) error { return r.db.Create(&e).Error } Now our problem is solved. We can pass the database connection to this struct and it is complying the interface as well so we can use Mock or Actual Repository to perform operations from services. I hope now it's clear. But if you are still confused let me know maybe I can record a video specifically based on Go Interfaces. Cheers!
Great sir 👍, sir will you be uploading one video per week.
I will try my best
great sir❤, waiting next video😊
This week we will cover authentication part with two episodes for members
I’m coming from C# so i’m a bit confused. Your userRepository struct only has the db variable and yes you are implementing the same functions in the interface{} but i never understood (learned go many times 😂) how by just emulating the interface functions there is a direct connection with the interface itself and not the db variable. I know not to think oop but it is still confusing
Hi Jay
I can understand your situation. I also built a lot of applications in C# in the past so we can not compare Object-Oriented (C#) with Procedural (Go) programming. Go does have the class concept and it does not work the way C# interfaces do. In Go you don't need to explicitly declare that a type implements an interface. Instead of if a type provides definitions for all the methods declared in an interface, Go complier will automatically consider that type as implementing the interface. Let's try to understand what's going on here:
Forget about the C# concept Go is a totally different language :D
We have our Interface that needs to be implemented but how we will do that? In this case, we need to implement the interface functions somehow so let's assume we can remove this part
("r userRepository") from the below function.
Now it looks good and we have implemented the interface but we have an issue here. Without type how we can pass the db connection so that the interface can perform the DB Operations as described in the interface?
// a function without type
func CreateBankAccount(e domain.BankAccount) error {
}
That's why we are creating a repository struct to use as a receiver function to implement the interface like below:
type userRepository struct {
db *gorm.DB
}
// function with type (userRepository struct)
func (r userRepository) CreateBankAccount(e domain.BankAccount) error {
return r.db.Create(&e).Error
}
Now our problem is solved. We can pass the database connection to this struct and it is complying the interface as well so we can use Mock or Actual Repository to perform operations from services.
I hope now it's clear. But if you are still confused let me know maybe I can record a video specifically based on Go Interfaces.
Cheers!