i am new to Go so i need to know the pattern and understand the logic in this code
first : in the car model folder lying a .go file name car.go but its struct was
and in controller folder there a cars.go file as following
type Car struct {
Id bson.ObjectId `bson:"_id"`
Model string `bson:"model" form:"" json:"model" binding:"required"`
Brand string `bson:"brand" form:"brand" json:"brand" binding:"required"`
CreatedAt bson.MongoTimestamp
}
type CarController struct {
carService *services.CarService
}
func (controller CarController) New() *CarController {
controller.carService = services.CarService{}.New()
return &controller
}
func (controller CarController) GetIndex(c *gin.Context) {
carList := controller.carService.Find(&bson.M{})
c.JSON(http.StatusOK, &carList)
//fmt.Println(carList) }
}
and in service folder was a carService.go file as following
type CarService struct {
dbName string
uri string
collectionName string
}
func (r CarService) New() *CarService {
configManger := viperconfing.Config{}
r.uri = configManger.GetConfig("dbUri")
r.dbName = configManger.GetConfig("dbName")
r.collectionName = "car"
return &r
}
func (r CarService) Find(query *bson.M) (cars []models.Car) {
session, _ := mgo.Dial(r.uri)
defer session.Close()
session.SetSafe(&mgo.Safe{})
collection := session.DB(r.dbName).C(r.collectionName)
collection.Find(query).All(&cars)
fmt.Println(cars)
return cars
}
I want to know the pattern used in this code i to understand the full logic and what is * meaning ??
*
is a pointer.
Please stop everything that you are doing and take the Go tour.