{"id":313696,"date":"2020-11-24T15:01:23","date_gmt":"2020-11-24T15:01:23","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=313696"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=313696","title":{"rendered":"\u041f\u0438\u0448\u0435\u043c CRUD-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043d\u0430 Go \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e Mysql, GORM, Echo, Clean Architecture"},"content":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\">\n<h2>\u041d\u0430\u0447\u043d\u0435\u043c \u0441\u043d\u0430\u0447\u0430\u043b\u0430<\/h2>\n<p>  \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0431\u0443\u0434\u0435\u0442 \u0441\u043a\u0430\u0437 \u043e \u0442\u043e\u043c, \u043a\u0430\u043a \u043d\u0430 Clean Architecture \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c API \u0441 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c\u0438 CR(U)D, \u0433\u0434\u0435 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0411\u0414 \u0432\u0437\u044f\u0442 Mysql, \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a \u2013 Echo, ORMapper \u2013 GORM.<\/p>\n<h2>\u0427\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u043c<\/h2>\n<p>  API \u0441 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c\u0438 Create, Read, (Update), Delete. \u041e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0441\u043e\u0431\u043e \u043d\u0435 \u0443\u0434\u0430\u043b\u043e\u0441\u044c, \u043c\u0438\u043b\u043e\u0441\u0442\u0438 \u043f\u0440\u043e\u0448\u0443 \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e.<\/p>\n<h2>\u0426\u0435\u043b\u0435\u0432\u0430\u044f \u0430\u0443\u0434\u0438\u0442\u043e\u0440\u0438\u044f<\/h2>\n<p>  \u0422\u0435 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0445\u043e\u0442\u044f\u0442 \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043f\u0440\u043e\u0441\u0442\u043e\u0439 API \u043f\u043e\u0441\u043b\u0435 \u043e\u0441\u0432\u043e\u0435\u043d\u0438\u044f Go.<a name=\"habracut\"><\/a><\/p>\n<h2>\u041e\u0441\u043d\u043e\u0432\u043d\u043e\u0435 c\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435<\/h2>\n<p>  <\/p>\n<h3>\u0427\u0442\u043e \u0442\u0430\u043a\u043e\u0435 Clean Architecture, \u0438 \u0441 \u0447\u0435\u043c \u0435\u0433\u043e \u0435\u0434\u044f\u0442<\/h3>\n<p>  \u042d\u0442\u043e \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0435:<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/lh\/k5\/ms\/lhk5mseuc85ktdik5rj4efthg2c.jpeg\" alt=\"image\"><\/p>\n<p>  \u0426\u0435\u043b\u044c Clean Architecture \u2013 \u044d\u0442\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u201c\u0441\u0444\u0435\u0440\u201d. \u0427\u0442\u043e\u0431\u044b \u0443\u0434\u0430\u0447\u043d\u043e \u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f \u044d\u0442\u043e\u0433\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u044f, \u043d\u0443\u0436\u043d\u043e \u0432\u0441\u0435\u0433\u0434\u0430 \u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0432 \u0433\u043e\u043b\u043e\u0432\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0441\u043b\u043e\u044f \u043d\u0430 \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0435 \u0441 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u043c\u0438. \u0420\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043d\u0430 \u0441\u043b\u043e\u0438 \u0443\u043b\u0443\u0447\u0448\u0430\u0435\u0442 \u0447\u0438\u0442\u0430\u0435\u043c\u043e\u0441\u0442\u044c \u043a\u043e\u0434\u0430 \u0438 \u0434\u0435\u043b\u0430\u0435\u0442 \u0435\u0433\u043e \u0443\u0441\u0442\u043e\u0439\u0447\u0438\u0432\u044b\u043c \u043a \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f\u043c.<\/p>\n<p>  \u041d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 \u0432\u044b\u0448\u0435 \u0441\u0442\u0440\u0435\u043b\u043a\u0430 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u0441\u043d\u0430\u0440\u0443\u0436\u0438 \u0432\u043d\u0443\u0442\u0440\u044c \u043a\u0440\u0443\u0433\u0430 \u2014 \u044d\u0442\u043e \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438. \u0412\u0430\u0436\u043d\u043e: \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u201c\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u044b\u201d \u0438\u0437\u0432\u043d\u0435 \u0432\u043d\u0443\u0442\u0440\u044c, \u043d\u0435 \u043d\u0430\u043e\u0431\u043e\u0440\u043e\u0442.<\/p>\n<p>  \u0414\u0440\u0443\u0433\u0438\u043c\u0438 \u0441\u043b\u043e\u0432\u0430\u043c\u0438, \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0441\u043d\u0430\u0440\u0443\u0436\u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0432\u0435\u0449\u0438, \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u043d\u044b\u0435 \u0432\u043d\u0443\u0442\u0440\u0438, \u043d\u043e \u0432\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u0442\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0432\u0435\u0449\u0438, \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u043d\u044b\u0435 \u0441\u043d\u0430\u0440\u0443\u0436\u0438, \u043d\u0430\u0445\u043e\u0434\u044f\u0441\u044c \u0432\u043d\u0443\u0442\u0440\u0438.<\/p>\n<p>  \u041a\u043e\u0434 \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043d\u0430\u043f\u0438\u0441\u0430\u043d \u0441 \u0443\u0447\u0435\u0442\u043e\u043c \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439, \u043a\u0430\u043a \u0438 \u0437\u0430\u0432\u0435\u0449\u0430\u0435\u0442 Clean Architecture.<\/p>\n<h3>\u041e \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u0445<\/h3>\n<p>  Endpoint\u2019\u044b \u043a\u0430\u0436\u0434\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435:<\/p>\n<p>  POST: \/users<br \/>  GET: \/users<br \/>  DELETE: \/users\/:id<\/p>\n<h3>\u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0439<\/h3>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/yl\/h1\/ik\/ylh1ikqwib7vvfp1lhpng2cun8u.png\" alt=\"image\"><\/p>\n<p>  \u0412\u0437\u0430\u0438\u043c\u043d\u043e\u0435 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0441\u043b\u043e\u0435\u0432 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u044b \u0438 \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0439 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:<\/p>\n<ul>\n<li>domain \u2014 Entities<\/li>\n<li>usecase \u2014 Use Cases<\/li>\n<li>interface \u2014 Controllers Presenters<\/li>\n<li>infrasctructure \u2014 External Interfaces<\/li>\n<\/ul>\n<p>  <\/p>\n<h3>domain<\/h3>\n<p>  \u0414\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0438 domain \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0441\u043b\u043e\u0439 Entity. \u0422\u0430\u043a \u043a\u0430\u043a \u043e\u043d \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0441\u0430\u043c\u043e\u043c \u044f\u0434\u0440\u0435, \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u043c\u043e\u0436\u0435\u0442 \u0441 \u043b\u044e\u0431\u043e\u0433\u043e \u0441\u043b\u043e\u044f.<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/zj\/k5\/7m\/zjk57mzvoym7xjswscajgkqli9w.png\" alt=\"image\"><\/p>\n<p>  src\/domain\/user.go  <\/p>\n<pre><code class=\"go\">package domain  type User struct {     ID   int    `json:&quot;id&quot; gorm:&quot;primary_key&quot;`     Name string `json:&quot;name&quot;` }<\/code><\/pre>\n<p>  \u0421\u043e\u0437\u0434\u0430\u0435\u043c struct User \u0441 id \u0438 \u0438\u043c\u0435\u043d\u0435\u043c \u0438 \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0435\u0440\u0432\u0438\u0447\u043d\u043e\u0433\u043e \u043a\u043b\u044e\u0447\u0430.<\/p>\n<p>  \u041d\u0435\u043c\u043d\u043e\u0433\u043e \u043e json:\u00abid\u00bb gorm:\u00abprimary_key\u00bb:<\/p>\n<p>  \u0412 json:\u00abid\u00bb json \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u0437\u0430 \u043c\u0430\u043f\u043f\u0438\u043d\u0433. \u0412 gorm:\u00abprimary_key\u00bb \u0438\u0434\u0435\u0442 \u043f\u043e\u043c\u0435\u0442\u043a\u0430 \u043d\u0430 \u043c\u043e\u0434\u0435\u043b\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e gorm.<br \/>  \u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u043f\u043e\u043c\u0438\u043c\u043e primary_key \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c not null, unique, default \u0438 \u0442.\u0434.,<\/p>\n<p>  \u041f\u043e\u043b\u0435\u0437\u043d\u0430\u044f \u0441\u0441\u044b\u043b\u043a\u0430 \u2013 <a href=\"https:\/\/gorm.io\/ru_RU\/docs\/models.html\" rel=\"nofollow\">\u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u0435 \u043c\u043e\u0434\u0435\u043b\u0435\u0439 \u043d\u0430 GORM<\/a>.<\/p>\n<h3>infrastructure<\/h3>\n<p>  \u0421\u0430\u043c\u044b\u0439 \u043a\u0440\u0430\u0439\u043d\u0438\u0439, \u0432\u043d\u0435\u0448\u043d\u0438\u0439 \u0441\u043b\u043e\u0439. \u0417\u0434\u0435\u0441\u044c \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0447\u0430\u0441\u0442\u044c, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u201c\u0432\u043d\u0435\u0448\u043d\u0438\u043c \u043c\u0438\u0440\u043e\u043c\u201d. \u0412 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u0432 \u044d\u0442\u043e\u043c \u0441\u043b\u043e\u0435 \u043e\u0431\u044a\u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0432\u044f\u0437\u044c \u0441 \u0411\u0414 \u0438 Router.<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/yz\/rk\/cy\/yzrkcy5v9uzw94jibdolcffhra4.png\" alt=\"image\"><\/p>\n<p>  \u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u044d\u0442\u043e \u0441\u0430\u043c\u044b\u0439 \u0432\u043d\u0435\u0448\u043d\u0438\u0439 \u0441\u043b\u043e\u0439, \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0435\u0433\u043e, \u043d\u0435 \u0438\u043c\u0435\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u043e \u043b\u044e\u0431\u043e\u043c \u0434\u0440\u0443\u0433\u043e\u043c \u0441\u043b\u043e\u0435.<\/p>\n<p>  src\/infrastucture\/sqlhandler.go  <\/p>\n<pre><code class=\"go\">package infrastructure  import (     &quot;gorm.io\/driver\/mysql&quot;     &quot;gorm.io\/gorm&quot;      &quot;echoSample\/src\/interfaces\/database&quot; )  type SqlHandler struct {     db *gorm.DB }  func NewSqlHandler() database.SqlHandler {     dsn := &quot;root:password@tcp(127.0.0.1:3306)\/go_sample?charset=utf8mb4&amp;parseTime=True&amp;loc=Local&quot;     db, err := gorm.Open(mysql.Open(dsn), &amp;gorm.Config{})     if err != nil {         panic(err.Error)     }     sqlHandler := new(SqlHandler)     sqlHandler.db = db     return sqlHandler }  func (handler *SqlHandler) Create(obj interface{}) {     handler.db.Create(obj) }  func (handler *SqlHandler) FindAll(obj interface{}) {     handler.db.Find(obj) }  func (handler *SqlHandler) DeleteById(obj interface{}, id string) {     handler.db.Delete(obj, id) }<\/code><\/pre>\n<p>  \u041f\u043e \u043f\u043e\u0432\u043e\u0434\u0443 \u0431\u0430\u0437 \u0434\u0430\u043d\u043d\u044b\u0445 \u044f \u043e\u0441\u0442\u0430\u0432\u043b\u044e \u0441\u0441\u044b\u043b\u043a\u0438 \u043d\u0430 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044e: <a href=\"https:\/\/gorm.io\/ru_RU\/docs\/create.html\" rel=\"nofollow\">gorm.io<\/a><\/p>\n<p>  \u0414\u0430\u043b\u0435\u0435 \u0438\u0434\u0435\u0442 \u043c\u0430\u0440\u0448\u0440\u0443\u0442\u0438\u0437\u0430\u0446\u0438\u044f.<\/p>\n<p>  \u0412 \u044d\u0442\u043e\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e \u0432\u0435\u0431-\u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a Echo. \u0424\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 Method \u0438 Path API. <a href=\"https:\/\/echo.labstack.com\/\" rel=\"nofollow\">\u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044e<\/a>.<\/p>\n<p>  src\/infrastructure\/router.go  <\/p>\n<pre><code class=\"go\">package infrastructure  import (     controllers &quot;echoSample\/src\/interfaces\/api&quot;     &quot;net\/http&quot;      &quot;github.com\/labstack\/echo&quot; )  func Init() {     \/\/ Echo instance     e := echo.New()     userController := controllers.NewUserController(NewSqlHandler())      e.GET(&quot;\/users&quot;, func(c echo.Context) error {         users := userController.GetUser()          c.Bind(&amp;users)          return c.JSON(http.StatusOK, users)     })      e.POST(&quot;\/users&quot;, func(c echo.Context) error {         userController.Create(c)         return c.String(http.StatusOK, &quot;created&quot;)     })      e.DELETE(&quot;\/users\/:id&quot;, func(c echo.Context) error {         id := c.Param(&quot;id&quot;)         userController.Delete(id)         return c.String(http.StatusOK, &quot;deleted&quot;)     })      \/\/ Start server     e.Logger.Fatal(e.Start(&quot;:1323&quot;)) }<\/code><\/pre>\n<p>  <\/p>\n<h3>interfaces<\/h3>\n<p>  \u0421\u043b\u043e\u0439 Controllers Presenters. <\/p>\n<p>  \u0412\u043e\u0442 \u0437\u0434\u0435\u0441\u044c \u0443\u0436\u0435 \u043d\u0443\u0436\u043d\u043e \u0432\u0441\u043f\u043e\u043c\u043d\u0438\u0442\u044c \u043e \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u044f\u0445.<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/ov\/a9\/u5\/ova9u5c51h0jvebalmq_luu_caw.png\" alt=\"image\"><\/p>\n<p>  \u041d\u0435\u0442 \u043f\u0440\u043e\u0431\u043b\u0435\u043c \u0441 \u0432\u044b\u0437\u043e\u0432\u043e\u043c \u0441\u043e \u0441\u043b\u043e\u0435\u0432 domain \u0438 usecase, \u043d\u043e \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u044b\u0437\u0432\u0430\u0442\u044c \u0441\u043b\u043e\u0439 infrastructure \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043e\u0431\u044a\u044f\u0432\u0438\u043c interface. (\u041f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0437\u0430\u043f\u0443\u0442\u0430\u043d\u043d\u043e, \u043d\u043e \u0440\u0435\u0447\u044c \u0438\u0434\u0435\u0442 \u043f\u0440\u043e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 sqlHandler, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0439 \u043d\u0430 \u0441\u043b\u043e\u0435 infrastrucure)<\/p>\n<p>  src\/interfaces\/api\/user_controller.go  <\/p>\n<pre><code class=\"go\">package controllers  import (     &quot;echoSample\/src\/domain&quot;     &quot;echoSample\/src\/interfaces\/database&quot;     &quot;echoSample\/src\/usecase&quot;      &quot;github.com\/labstack\/echo&quot; )  type UserController struct {     Interactor usecase.UserInteractor }  func NewUserController(sqlHandler database.SqlHandler) *UserController {     return &amp;UserController{         Interactor: usecase.UserInteractor{             UserRepository: &amp;database.UserRepository{                 SqlHandler: sqlHandler,             },         },     } }  func (controller *UserController) Create(c echo.Context) {     u := domain.User{}     c.Bind(&amp;u)     controller.Interactor.Add(u)     createdUsers := controller.Interactor.GetInfo()     c.JSON(201, createdUsers)     return }  func (controller *UserController) GetUser() []domain.User {     res := controller.Interactor.GetInfo()     return res }  func (controller *UserController) Delete(id string) {     controller.Interactor.Delete(id) }<\/code><\/pre>\n<p>  \u0412 controller \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c \u0441\u043e \u0441\u043b\u043e\u0451\u0432 domain \u0438 usecase, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0440\u043e\u0431\u043b\u0435\u043c \u043d\u0435\u0442.<\/p>\n<p>  src\/interfaces\/api\/context.go  <\/p>\n<pre><code class=\"go\">package controllers  type Context interface {     Param(string) string     Bind(interface{}) error     Status(int)     JSON(int, interface{}) }<\/code><\/pre>\n<p>  \u0421\u0432\u044f\u0437\u044c \u0441 \u0411\u0414<\/p>\n<p>  src\/interfaces\/database\/user_repository.go  <\/p>\n<pre><code class=\"go\">package database package database  import (     &quot;echoSample\/src\/domain&quot; )  type UserRepository struct {     SqlHandler }  func (db *UserRepository) Store(u domain.User) {     db.Create(&amp;u) }  func (db *UserRepository) Select() []domain.User {     user := []domain.User{}     db.FindAll(&amp;user)     return user } func (db *UserRepository) Delete(id string) {     user := []domain.User{}     db.DeleteById(&amp;user, id) }<\/code><\/pre>\n<p>  \u0412 repository \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f sqlHandler, \u043d\u043e \u043e\u043d \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0435 \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0441\u043e \u0441\u043b\u043e\u044f infrastructure, \u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u0442\u0430\u043c \u0436\u0435 interface.<\/p>\n<p>  \u042d\u0442\u043e \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u043e\u043c \u0438\u043d\u0432\u0435\u0440\u0441\u0438\u0438 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439.<\/p>\n<p>  src\/interfaces\/db\/sql_handler.go  <\/p>\n<pre><code class=\"go\">package database  type SqlHandler interface {     Create(object interface{})     FindAll(object interface{})     DeleteById(object interface{}, id string) }<\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u0446\u0435\u0441\u0441 sql_handler.<\/p>\n<h3>usecase<\/h3>\n<p>  \u041f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u043e\u0441\u0442\u0430\u0432\u0448\u0438\u0439\u0441\u044f \u0441\u043b\u043e\u0439, usecase.<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/kr\/3y\/tz\/kr3ytzdpewil4vx6t_jcm3oqeey.png\" alt=\"image\"><\/p>\n<p>  src\/usecase\/user_interactor.go  <\/p>\n<pre><code class=\"go\">package usecase  import &quot;echoSample\/src\/domain&quot;  type UserInteractor struct {     UserRepository UserRepository }  func (interactor *UserInteractor) Add(u domain.User) {     interactor.UserRepository.Store(u) }  func (interactor *UserInteractor) GetInfo() []domain.User {     return interactor.UserRepository.Select() }  func (interactor *UserInteractor) Delete(id string) {     interactor.UserRepository.Delete(id) }<\/code><\/pre>\n<p>  \u041e\u043f\u044f\u0442\u044c \u0436\u0435, \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c \u043f\u0440\u0438\u043d\u0446\u0438\u043f \u0438\u043d\u0432\u0435\u0440\u0441\u0438\u0438 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438, \u043a\u0430\u043a \u0438 \u0440\u0430\u043d\u044c\u0448\u0435. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c user_repository.go.<\/p>\n<p>  src\/usecase\/user_repository.go  <\/p>\n<pre><code class=\"go\">package usecase  import (     &quot;echoSample\/src\/domain&quot; )  type UserRepository interface {     Store(domain.User)     Select() []domain.User     Delete(id string) }<\/code><\/pre>\n<p>  \u041d\u0430 \u044d\u0442\u043e\u043c \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0430. <\/p>\n<p>  \u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u0435 mysql \u0441 docker-compose.yml, \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u0435 \u0441\u0435\u0440\u0432\u0435\u0440, \u0438 \u0432\u0441\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c.<\/p>\n<pre><code class=\"plaintext\">docker-compose.yml version: &quot;3.6&quot; services:   db:     image: mysql:5.7     container_name: go_sample     volumes:       # \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 mysql       - .\/mysql\/conf:\/etc\/mysql\/conf.d       - .\/mysql\/data:\/var\/lib\/mysql     command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci     ports:       - 3306:3306     environment:       MYSQL_DATABASE: go_sample       MYSQL_ROOT_PASSWORD: password       MYSQL_USER: root       TZ: &quot;Asia\/Tokyo&quot;<\/code><\/pre>\n<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/529086\/\"> https:\/\/habr.com\/ru\/post\/529086\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\">\n<h2>\u041d\u0430\u0447\u043d\u0435\u043c \u0441\u043d\u0430\u0447\u0430\u043b\u0430<\/h2>\n<p>  \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0431\u0443\u0434\u0435\u0442 \u0441\u043a\u0430\u0437 \u043e \u0442\u043e\u043c, \u043a\u0430\u043a \u043d\u0430 Clean Architecture \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c API \u0441 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c\u0438 CR(U)D, \u0433\u0434\u0435 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0411\u0414 \u0432\u0437\u044f\u0442 Mysql, \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a \u2013 Echo, ORMapper \u2013 GORM.<\/p>\n<h2>\u0427\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u043c<\/h2>\n<p>  API \u0441 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c\u0438 Create, Read, (Update), Delete. \u041e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0441\u043e\u0431\u043e \u043d\u0435 \u0443\u0434\u0430\u043b\u043e\u0441\u044c, \u043c\u0438\u043b\u043e\u0441\u0442\u0438 \u043f\u0440\u043e\u0448\u0443 \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e.<\/p>\n<h2>\u0426\u0435\u043b\u0435\u0432\u0430\u044f \u0430\u0443\u0434\u0438\u0442\u043e\u0440\u0438\u044f<\/h2>\n<p>  \u0422\u0435 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0445\u043e\u0442\u044f\u0442 \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043f\u0440\u043e\u0441\u0442\u043e\u0439 API \u043f\u043e\u0441\u043b\u0435 \u043e\u0441\u0432\u043e\u0435\u043d\u0438\u044f Go.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-313696","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/313696","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=313696"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/313696\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=313696"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=313696"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=313696"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}