Boost Your Go Projects: Efficient Database Techniques Revealed

แชร์
ฝัง
  • เผยแพร่เมื่อ 7 พ.ย. 2024

ความคิดเห็น • 6

  • @AnuragMishra-ws4zc
    @AnuragMishra-ws4zc 9 หลายเดือนก่อน +1

    Great sir 👍, sir will you be uploading one video per week.

    • @codewithjay
      @codewithjay  9 หลายเดือนก่อน

      I will try my best

  • @DevWithRian
    @DevWithRian 9 หลายเดือนก่อน

    great sir❤, waiting next video😊

    • @codewithjay
      @codewithjay  9 หลายเดือนก่อน

      This week we will cover authentication part with two episodes for members

  • @jaymartinez311
    @jaymartinez311 5 หลายเดือนก่อน

    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

    • @codewithjay
      @codewithjay  4 หลายเดือนก่อน +1

      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!