<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Juliano Silva on Medium]]></title>
        <description><![CDATA[Stories by Juliano Silva on Medium]]></description>
        <link>https://medium.com/@julianojj?source=rss-b68da1dfc25b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*1Sd9Ywv9XfxTbzezQ15E6Q.jpeg</url>
            <title>Stories by Juliano Silva on Medium</title>
            <link>https://medium.com/@julianojj?source=rss-b68da1dfc25b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 27 Nov 2026 22:06:11 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@julianojj/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Exploring the Advantages of Slog on Golang: An Overview]]></title>
            <link>https://medium.com/@julianojj/exploring-the-advantages-of-slog-on-golang-an-overview-2cbee6fece07?source=rss-b68da1dfc25b------2</link>
            <guid isPermaLink="false">https://medium.com/p/2cbee6fece07</guid>
            <category><![CDATA[go]]></category>
            <category><![CDATA[logs]]></category>
            <category><![CDATA[software-engineer]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[golang]]></category>
            <dc:creator><![CDATA[Juliano Silva]]></dc:creator>
            <pubDate>Mon, 13 Nov 2023 13:04:33 GMT</pubDate>
            <atom:updated>2023-11-13T13:04:33.579Z</atom:updated>
            <content:encoded><![CDATA[<p>Slog is a new feature to work with native log on Golang.</p><p>In this article, I will guide you through the process of establishing a well-organized registry using best practices.</p><h4>How to using slog?</h4><p>To using slog, you should import a package log/slog or install based on <a href="http://To using slog, you should import a package log/slog or install based on documentation here.">documentation</a>.</p><pre>import &quot;log/slog&quot;<br><br>func main() {<br>  slog.Info(&quot;Test&quot;) // 2023/11/13 09:01:14 INFO Test<br>}</pre><p>By default, the log using a text output log.</p><h4>Create a custom slog to using JSON output</h4><p>Basically we should use custom options for slog instance. Show the example below.</p><pre>type Logging struct {<br> config *config.Config<br> logger *slog.Logger<br>}<br><br>func InitializerLogging(<br> config *config.Config,<br>) *Logging {<br> opts := &amp;slog.HandlerOptions{Level: parseLevel(config.APPLICATION_LOG_LEVEL)}<br> jsonHandler := slog.NewJSONHandler(os.Stdout, opts)<br> logger := slog.New(jsonHandler).With(<br>  &quot;application_metadata&quot;,<br>  map[string]any{<br>   &quot;name&quot;:        config.APPLICATION_NAME,<br>   &quot;environment&quot;: config.APPLICATION_ENVIRONMENT,<br>  },<br> )<br> return &amp;Logging{<br>  config,<br>  logger,<br> }<br>}<br><br>func (l *Logging) Info(message string, data any) {<br> l.logger.Info(message, slog.Any(&quot;data&quot;, data))<br>}<br><br>func (l *Logging) Warn(message string, data any) {<br> l.logger.Warn(message, slog.Any(&quot;data&quot;, data))<br>}<br><br>func (l *Logging) Error(message string, data any) {<br> l.logger.Error(message, slog.Any(&quot;data&quot;, data))<br>}<br><br>func (l *Logging) Debug(message string, data any) {<br> l.logger.Debug(message, slog.Any(&quot;data&quot;, data))<br>}<br><br>func parseLevel(level string) slog.Level {<br> switch strings.ToLower(level) {<br> case &quot;error&quot;:<br>  return slog.LevelError<br> case &quot;warn&quot;:<br>  return slog.LevelWarn<br> case &quot;debug&quot;:<br>  return slog.LevelDebug<br> default:<br>  return slog.LevelInfo<br> }<br>}</pre><h4>Run example</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/563/1*4YV-elhykJXwZXwwmMIoJA.png" /><figcaption>Run initialize logger.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IxO3cB7P9EBhPs66cHA9HQ.png" /><figcaption>Output logs.</figcaption></figure><h4>Conclusion</h4><p>This article explores the benefits of Slog, a new feature for native log management in Golang. The focus is on establishing a well-organized registry using best practices. To use Slog, one should import the package log/slog or follow the installation instructions in the documentation. By default, Slog generates text output logs, but customization is key. The article demonstrates creating a custom Slog configuration for JSON output, showcasing the flexibility of Slog for diverse logging needs in Golang projects.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2cbee6fece07" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Data Persistence Patterns in Golang]]></title>
            <link>https://medium.com/@julianojj/data-persistence-patterns-in-golang-1d6b803dc051?source=rss-b68da1dfc25b------2</link>
            <guid isPermaLink="false">https://medium.com/p/1d6b803dc051</guid>
            <category><![CDATA[backend-development]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[design-patterns]]></category>
            <category><![CDATA[software-engineering]]></category>
            <dc:creator><![CDATA[Juliano Silva]]></dc:creator>
            <pubDate>Fri, 13 Oct 2023 11:23:55 GMT</pubDate>
            <atom:updated>2023-10-13T11:54:42.004Z</atom:updated>
            <content:encoded><![CDATA[<p>Data persistence patterns are ready-made solutions to common problems in data persistence. They can help make your code more reusable, flexible, and easy to maintain.</p><p>In Golang, there are several data persistence patterns available. Some of the most popular include:</p><ul><li><strong>Repository</strong>: Mediates between the domain and data mapping layers using a collection-like interface for accessing domain objects.</li><li><strong>Data Access Object</strong>: This pattern abstracts the logic for accessing a specific table in a database.</li><li><strong>Active Record</strong>: This pattern combines business logic with data access logic.</li></ul><p>Many devs get confused when it comes to Repository and DAO, they think that every means of persistence is a repository, but it is not. Both originate from the Gateway pattern, but the main difference between them is that the repository persists domain objects and the DAO persists data transfer objects.</p><h4>Example of repository:</h4><p>Repository persists domain objects. In application layer, should convert DTOs to domain object.</p><pre>package domain<br><br>import (<br> &quot;errors&quot;<br> &quot;net/mail&quot;<br> &quot;strings&quot;<br>)<br><br>type Customer struct {<br> Id          string<br> Name        string<br> Email       string<br> PhoneNumber string<br>}<br><br>func NewCustomer(<br> id, name, email, phoneNumber string,<br>) *Customer {<br> return &amp;Customer{<br>  Id:          id,<br>  Name:        name,<br>  Email:       email,<br>  PhoneNumber: phoneNumber,<br> }<br>}<br><br>func (c *Customer) Validate() error {<br> if isInvalidName(c.Name) {<br>  return errors.New(&quot;invalid name&quot;)<br> }<br> if isInvalidEmail(c.Email) {<br>  return errors.New(&quot;invalid email&quot;)<br> }<br> return nil<br>}<br><br>func isInvalidName(name string) bool {<br> splitedName := strings.Split(name, &quot; &quot;)<br> return len(splitedName) &lt; 2<br>}<br><br>func isInvalidEmail(email string) bool {<br> _, err := mail.ParseAddress(email)<br> return err != nil<br>}</pre><pre>package domain<br><br>type CustomerRepository interface {<br> Save(customer *Customer) error<br> FindById(customerId string) (*Customer, error)<br>}</pre><h4>Example of Data Access Object:</h4><p>See that the object has no behaviors, only attributes. DAO allows isolated operation. Update via email, for example.</p><pre>type CustomerModel struct {<br> Id          string `json:&quot;id&quot;`<br> Name        string `json:&quot;name&quot;`<br> Email       string `json:&quot;email&quot;`<br> PhoneNumber string `json:&quot;phone_number&quot;`<br>}</pre><pre>type CustomerDAO interface {<br> Save(input *CustomerModel) error<br> UpdateEmail(email string) error<br>}</pre><h4>Example of Active Record:</h4><p>This approach is little used, I personally don’t like mixing business logic with databases. This ends up generating high coupling between the application and the database, but for small projects it’s not a bad idea. This pattern is well used by the Ruby community.</p><pre>type Customer struct {<br> Id          string<br> Name        string<br> Email       string<br> PhoneNumber string<br> DB          *sql.DB<br>}<br><br>func NewCustomer(<br> id, name, email, phoneNumber string,<br> db *sql.DB,<br>) *Customer {<br> return &amp;Customer{<br>  Id:          id,<br>  Name:        name,<br>  Email:       email,<br>  PhoneNumber: phoneNumber,<br>  DB:          db,<br> }<br>}<br><br>func (c *Customer) Save() error {<br> _, err := c.DB.Exec(<br>  &quot;INSERT INTO Users(Id, Name, Email, PhoneNumber) VALUES($1, $2, $3, $4)&quot;,<br>  c.Id,<br>  c.Name,<br>  c.Email,<br>  c.PhoneNumber,<br> )<br> if err != nil {<br>  return err<br> }<br> return nil<br>}</pre><h4>Conclusion</h4><p>Data persistence patterns are a valuable tool for any Golang developer that needs to store data. They can help make your code more reusable, flexible, and easy to maintain.</p><p>The two most popular data persistence patterns in Golang are the repository pattern and the DAO pattern. The repository pattern is more flexible and scalable than the DAO pattern, and it is generally a better choice for most applications.</p><p>However, the DAO pattern can be useful for simple applications, and it may be a better choice for developers who are already familiar with it.</p><p>Ultimately, the best way to decide which data persistence pattern to use is to consider the specific needs of your application.</p><h4>More Information</h4><ul><li><a href="https://martinfowler.com/eaaCatalog/repository.html">Repository</a></li><li><a href="https://martinfowler.com/eaaCatalog/tableDataGateway.html">Table Data Gateway</a></li><li><a href="https://www.martinfowler.com/eaaCatalog/activeRecord.html">Active Record</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1d6b803dc051" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How To Apply Clean Architecture in Golang: A Step-by-Step Guide]]></title>
            <link>https://medium.com/@julianojj/how-to-apply-clean-architecture-in-golang-a-step-by-step-guide-23ce0ed2e1d9?source=rss-b68da1dfc25b------2</link>
            <guid isPermaLink="false">https://medium.com/p/23ce0ed2e1d9</guid>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[test-driven-development]]></category>
            <category><![CDATA[domain-driven-design]]></category>
            <category><![CDATA[clean-architecture]]></category>
            <category><![CDATA[best-practices]]></category>
            <dc:creator><![CDATA[Juliano Silva]]></dc:creator>
            <pubDate>Sat, 13 May 2023 13:06:34 GMT</pubDate>
            <atom:updated>2023-05-13T13:07:25.875Z</atom:updated>
            <content:encoded><![CDATA[<p>How To Apply Clean Architecture in Golang: A Step-by-Step Guide</p><p>1. Introduction:</p><p>Clean Architecture is a design pattern that emphasizes separation of concerns and a clear separation of layers in your application. Promotes code maintainability and scalability.</p><p>2. Building Your Project Directory:</p><p>I using a structure based on Core and Infra. Core have a business rules, application rules and validations. In Infra layer have a external system or resource how: adapters, repositories, APIs, Queues and others. But don’t worry about it, each project have one necessity.</p><p>3. Implementing Clean Architecture in GoLang:</p><p>Example of entity</p><pre>package domain<br><br>import (<br> &quot;io&quot;<br><br> &quot;github.com/julianojj/aurora/internal/core/exceptions&quot;<br>)<br><br>type File struct {<br> FileID   string<br> Name     string<br> FileType string<br> Size     int64<br> Reader   io.Reader<br> Path     string<br>}<br><br>func NewFile(fileID, name, fileType, path string, size int64, reader io.Reader) (*File, error) {<br> file := &amp;File{<br>  FileID:   fileID,<br>  Name:     name,<br>  FileType: fileType,<br>  Path:     path,<br>  Size:     size,<br>  Reader:   reader,<br> }<br> err := file.Validate()<br> if err != nil {<br>  return nil, err<br> }<br> return file, nil<br>}<br><br>func (f *File) Validate() error {<br> if f.FileID == &quot;&quot; {<br>  return exceptions.NewValidationException(&quot;File ID cannot be empty&quot;)<br> }<br> if f.Name == &quot;&quot; {<br>  return exceptions.NewValidationException(&quot;Name cannot be empty&quot;)<br> }<br> if f.FileType == &quot;&quot; {<br>  return exceptions.NewValidationException(&quot;File type cannot be empty&quot;)<br> }<br> if f.IsInvalidType() {<br>  return exceptions.NewValidationException(&quot;File type not supported&quot;)<br> }<br> if f.Size &lt;= 0 {<br>  return exceptions.NewNotFoundException(&quot;File size must be greater than 0&quot;)<br> }<br> if f.Reader == nil {<br>  return exceptions.NewNotFoundException(&quot;File reader cannot be empty&quot;)<br> }<br> if f.Path == &quot;&quot; {<br>  return exceptions.NewValidationException(&quot;Path cannot be empty&quot;)<br> }<br> return nil<br>}<br><br>func (f *File) IsInvalidType() bool {<br> return f.FileType != &quot;image/jpeg&quot; &amp;&amp; f.FileType != &quot;image/png&quot;<br>}</pre><p>Based on DDD, i have a entity content your validations. I need create a unit test for rules validation expected.</p><p>Example of exception</p><pre>package exceptions<br><br>type ValidationException struct {<br> Message string<br> Code    int<br>}<br><br>func NewValidationException(message string) *ValidationException {<br> return &amp;ValidationException{<br>  Message: message,<br>  Code:    400,<br> }<br>}<br><br>func (e *ValidationException) Error() string {<br> return e.Message<br>}</pre><p>I use a extends error validation, after create, we can using in entities and use cases.</p><p>Example of usecases</p><pre>package usecases<br><br>import (<br> &quot;fmt&quot;<br> &quot;io&quot;<br> &quot;os&quot;<br> &quot;path&quot;<br><br> &quot;github.com/google/uuid&quot;<br> &quot;github.com/julianojj/aurora/internal/core/domain&quot;<br> &quot;github.com/julianojj/aurora/internal/infra/adapters&quot;<br>)<br><br>type UploadFile struct {<br> fileRepository domain.FileRepository<br> bucket         adapters.Bucket<br>}<br><br>type UploadFileInput struct {<br> Name     string    `json:&quot;name&quot;`<br> Mimetype string    `json:&quot;mimetype&quot;`<br> Size     int64     `json:&quot;size&quot;`<br> Reader   io.Reader `json:&quot;reader&quot;`<br>}<br><br>func NewUploadFile(fileRepository domain.FileRepository, bucket adapters.Bucket) *UploadFile {<br> return &amp;UploadFile{<br>  fileRepository,<br>  bucket,<br> }<br>}<br><br>func (u *UploadFile) Execute(input UploadFileInput) error {<br> fileID := uuid.NewString()<br> bucketName := os.Getenv(&quot;MINIO_BUCKET_NAME&quot;)<br> ext := path.Ext(input.Name)<br> newName := fmt.Sprintf(&quot;%s%s&quot;, fileID, ext)<br> file, err := domain.NewFile(<br>  fileID,<br>  newName,<br>  input.Mimetype,<br>  fmt.Sprintf(&quot;/%s/%s&quot;, bucketName, newName),<br>  input.Size,<br>  input.Reader,<br> )<br> if err != nil {<br>  return err<br> }<br> err = u.fileRepository.Save(file)<br> if err != nil {<br>  return nil<br> }<br> err = u.bucket.PutObject(file)<br> if err != nil {<br>  return err<br> }<br> return nil<br>}</pre><p>The use case is responsible to drive a entities, exceptions and infra. In the example a file upload usecase use a file entity and bucket service and repository.</p><p>Example of infra</p><pre>package adapters<br><br>import (<br> &quot;github.com/julianojj/aurora/internal/core/domain&quot;<br>)<br><br>type Bucket interface {<br> CreateBucket() error<br> PutObject(file *domain.File) error<br>}</pre><p>Using a dependency injection, usecase have an interface adapter wen send an concrete implementation</p><p>4. Testing Your Application:</p><p>Unit test</p><pre>package domain<br><br>import (<br> &quot;bytes&quot;<br> &quot;testing&quot;<br><br> &quot;github.com/stretchr/testify/assert&quot;<br>)<br><br>func TestNotShouldCreateFileIfEmptyFileID(t *testing.T) {<br> _, err := NewFile(<br>  &quot;&quot;,<br>  &quot;test&quot;,<br>  &quot;image/jpeg&quot;,<br>  &quot;/aurora/test.jpg&quot;,<br>  1,<br>  bytes.NewReader([]byte(&quot;&quot;)),<br> )<br> assert.EqualError(t, err, &quot;File ID cannot be empty&quot;)<br>}<br><br>func TestNotShouldCreateFileIfEmptyName(t *testing.T) {<br> _, err := NewFile(<br>  &quot;1&quot;,<br>  &quot;&quot;,<br>  &quot;image/jpeg&quot;,<br>  &quot;/aurora/test.jpg&quot;,<br>  1,<br>  bytes.NewReader([]byte(&quot;&quot;)),<br> )<br> assert.EqualError(t, err, &quot;Name cannot be empty&quot;)<br>}<br><br>func TestNotShouldCreateFileIfEmptyFileType(t *testing.T) {<br> _, err := NewFile(<br>  &quot;1&quot;,<br>  &quot;test&quot;,<br>  &quot;&quot;,<br>  &quot;/aurora/test.jpg&quot;,<br>  1,<br>  bytes.NewReader([]byte(&quot;&quot;)),<br> )<br> assert.EqualError(t, err, &quot;File type cannot be empty&quot;)<br>}<br><br>func TestNotShouldCreateFileIfEmptyPath(t *testing.T) {<br> _, err := NewFile(<br>  &quot;1&quot;,<br>  &quot;test&quot;,<br>  &quot;image/jpeg&quot;,<br>  &quot;&quot;,<br>  1,<br>  bytes.NewReader([]byte(&quot;&quot;)),<br> )<br> assert.EqualError(t, err, &quot;Path cannot be empty&quot;)<br>}<br><br>func TestNotShouldCreateFileIfInvalidFileType(t *testing.T) {<br> _, err := NewFile(<br>  &quot;1&quot;,<br>  &quot;test&quot;,<br>  &quot;image/test&quot;,<br>  &quot;/aurora/image.jpg&quot;,<br>  1,<br>  bytes.NewReader([]byte(&quot;&quot;)),<br> )<br> assert.EqualError(t, err, &quot;File type not supported&quot;)<br>}<br><br>func TestNotShouldCreateFileIfNegativeSize(t *testing.T) {<br> _, err := NewFile(<br>  &quot;1&quot;,<br>  &quot;test&quot;,<br>  &quot;image/jpeg&quot;,<br>  &quot;/aurora/image.jpg&quot;,<br>  0,<br>  bytes.NewReader([]byte(&quot;&quot;)),<br> )<br> assert.EqualError(t, err, &quot;File size must be greater than 0&quot;)<br>}<br><br>func TestNotShouldCreateFileIfEmptyReader(t *testing.T) {<br> _, err := NewFile(<br>  &quot;1&quot;,<br>  &quot;test&quot;,<br>  &quot;image/jpeg&quot;,<br>  &quot;/aurora/image.jpg&quot;,<br>  1,<br>  nil,<br> )<br> assert.EqualError(t, err, &quot;File reader cannot be empty&quot;)<br>}<br><br>func TestShouldCreateFile(t *testing.T) {<br> file, _ := NewFile(<br>  &quot;1&quot;,<br>  &quot;test&quot;,<br>  &quot;image/jpeg&quot;,<br>  &quot;/aurora/test.jpg&quot;,<br>  1,<br>  bytes.NewReader([]byte(&quot;&quot;)),<br> )<br> assert.Equal(t, &quot;test&quot;, file.Name)<br>}</pre><p>In the test i have a tests for file entity, when testes business rules.</p><p>Integration test</p><pre>package usecases<br><br>import (<br> &quot;bytes&quot;<br> &quot;testing&quot;<br><br> &quot;github.com/julianojj/aurora/internal/infra/adapters&quot;<br> &quot;github.com/julianojj/aurora/internal/infra/repository&quot;<br> &quot;github.com/stretchr/testify/assert&quot;<br>)<br><br>func TestShouldUploadFile(t *testing.T) {<br> fileRepository := repository.NewFileRepositoryMemory()<br> bucket := adapters.NewFakeBucket()<br> uploadFile := NewUploadFile(fileRepository, bucket)<br> file := bytes.NewReader([]byte(&quot;test&quot;))<br> input := UploadFileInput{<br>  Name:     &quot;test&quot;,<br>  Size:     123,<br>  Mimetype: &quot;image/jpeg&quot;,<br>  Reader:   file,<br> }<br> err := uploadFile.Execute(input)<br> assert.NoError(t, err)<br> files, _ := fileRepository.FindAll()<br> assert.Len(t, files, 1)<br>}</pre><p>In the test i have a tests for upload usecase, the testes an integration of parts of layers.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/426/1*WHlCl6Pa3ekwEjUgnn6oVA.png" /><figcaption>Run all tests</figcaption></figure><p>5. Conclusion:</p><p>Clean Architecture promotes maintainable code, and GoLang is perfect for it. Follow the steps outlined in the post for implementing Clean Architecture in GoLang, including understanding the principles, building your directory, and testing your application. By doing so, you’ll be able to create high-quality, maintainable software. Prioritize Clean Architecture for building robust applications that are easy to maintain and scale over time.</p><p>More information:</p><ul><li><a href="https://github.com/julianojj/aurora">GitHub - julianojj/aurora: Aurora is an online editor, be an artist.</a></li><li><a href="https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html">Clean Coder Blog</a></li><li><a href="https://go.dev/">The Go Programming Language</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=23ce0ed2e1d9" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>