Junior — Middle
What is the purpose of the repository directory in a project?
sobes.tech AI
Answer from AI
In Go projects, the repository directory is usually used to implement the data access layer. It contains code responsible for interacting with the database or other data stores.
Main tasks of the repository directory:
- Abstracting data access logic from business logic.
- Providing interfaces for CRUD operations (create, read, update, delete).
- Simplifying testing by allowing repositories to be replaced with mocks.
Example of a repository interface:
package repository
type User struct {
ID int
Name string
}
type UserRepository interface {
GetByID(id int) (*User, error)
Save(user *User) error
}
Thus, the repository directory helps structure the project and separate data handling logic from the rest of the code.