- We define the User struct representing a user entity.
- We define the UserRepository interface with methods for managing users.
- We implement InMemoryUserRepository and MySQLUserRepository to provide in-memory and MySQL-based repositories, respectively.
- NewInMemoryUserRepository and NewMySQLUserRepository are constructor functions for creating instances of the respective repositories.
- We demonstrate how to use both repositories in the main function by inserting users, getting a user by ID, and getting all users.
packagemainimport("database/sql""errors""fmt"_"github.com/go-sql-driver/mysql")// User represents a user entitytypeUserstruct{IDintUsernamestringEmailstring}// UserRepository defines the methods a user repository must implementtypeUserRepositoryinterface{Insert(user*User)errorGetByID(idint)(*User,error)GetAll()([]*User,error)}// InMemoryUserRepository is an in-memory implementation of UserRepositorytypeInMemoryUserRepositorystruct{users[]*User}// NewInMemoryUserRepository creates a new instance of InMemoryUserRepositoryfuncNewInMemoryUserRepository()*InMemoryUserRepository{return&InMemoryUserRepository{users:make([]*User,0),}}// Insert inserts a new user into the repositoryfunc(repo*InMemoryUserRepository)Insert(user*User)error{repo.users=append(repo.users,user)returnnil}// GetByID retrieves a user by its ID from the repositoryfunc(repo*InMemoryUserRepository)GetByID(idint)(*User,error){for_,user:=rangerepo.users{ifuser.ID==id{returnuser,nil}}returnnil,errors.New("user not found")}// GetAll retrieves all users from the repositoryfunc(repo*InMemoryUserRepository)GetAll()([]*User,error){returnrepo.users,nil}// MySQLUserRepository is a MySQL implementation of UserRepositorytypeMySQLUserRepositorystruct{db*sql.DB}// NewMySQLUserRepository creates a new instance of MySQLUserRepositoryfuncNewMySQLUserRepository(dataSourceNamestring)(*MySQLUserRepository,error){db,err:=sql.Open("mysql",dataSourceName)iferr!=nil{returnnil,err}return&MySQLUserRepository{db:db,},nil}// Insert inserts a new user into the MySQL repositoryfunc(repo*MySQLUserRepository)Insert(user*User)error{_,err:=repo.db.Exec("INSERT INTO users (id, username, email) VALUES (?, ?, ?)",user.ID,user.Username,user.Email)iferr!=nil{returnerr}returnnil}// GetByID retrieves a user by its ID from the MySQL repositoryfunc(repo*MySQLUserRepository)GetByID(idint)(*User,error){row:=repo.db.QueryRow("SELECT id, username, email FROM users WHERE id = ?",id)user:=&User{}err:=row.Scan(&user.ID,&user.Username,&user.Email)iferr!=nil{returnnil,err}returnuser,nil}// GetAll retrieves all users from the MySQL repositoryfunc(repo*MySQLUserRepository)GetAll()([]*User,error){rows,err:=repo.db.Query("SELECT id, username, email FROM users")iferr!=nil{returnnil,err}deferrows.Close()varusers[]*Userforrows.Next(){user:=&User{}err:=rows.Scan(&user.ID,&user.Username,&user.Email)iferr!=nil{returnnil,err}users=append(users,user)}returnusers,nil}funcmain(){// Example usage of in-memory repositorymemRepo:=NewInMemoryUserRepository()memRepo.Insert(&User{ID:1,Username:"user1",Email:"user1@example.com"})memRepo.Insert(&User{ID:2,Username:"user2",Email:"user2@example.com"})user,err:=memRepo.GetByID(1)iferr!=nil{fmt.Println("Error:",err)}else{fmt.Println("In-Memory User:",user)}allUsers,err:=memRepo.GetAll()iferr!=nil{fmt.Println("Error:",err)}else{fmt.Println("In-Memory All Users:",allUsers)}// Example usage of MySQL repositorymysqlRepo,err:=NewMySQLUserRepository("user:password@tcp(127.0.0.1:3306)/database_name")iferr!=nil{fmt.Println("Error:",err)return}mysqlRepo.Insert(&User{ID:3,Username:"user3",Email:"user3@example.com"})mysqlRepo.Insert(&User{ID:4,Username:"user4",Email:"user4@example.com"})user,err=mysqlRepo.GetByID(3)iferr!=nil{fmt.Println("Error:",err)}else{fmt.Println("MySQL User:",user)}allUsers,err=mysqlRepo.GetAll()iferr!=nil{fmt.Println("Error:",err)}else{fmt.Println("MySQL All Users:",allUsers)}}
Top comments (0)
Subscribe
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (0)