<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Omnia Wahid</title>
    <description>The latest articles on DEV Community by Omnia Wahid (@omniawahid).</description>
    <link>https://dev.to/omniawahid</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F789446%2F606d6a17-adda-4c3b-9027-6a49900367f5.jpeg</url>
      <title>DEV Community: Omnia Wahid</title>
      <link>https://dev.to/omniawahid</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/omniawahid"/>
    <language>en</language>
    <item>
      <title>Rollback Functions in Golang</title>
      <dc:creator>Omnia Wahid</dc:creator>
      <pubDate>Tue, 22 Feb 2022 21:42:06 +0000</pubDate>
      <link>https://dev.to/omniawahid/rollback-functions-in-golang-7e9</link>
      <guid>https://dev.to/omniawahid/rollback-functions-in-golang-7e9</guid>
      <description>&lt;p&gt;&lt;strong&gt;Errors in Go&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Go methods can return multiple values. One of these values can be an error. The zero value of an error is nil. After the call of each method, you check the error value with nil. If it is not equal to nil, then an error has occurred.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to Implement a Rollback Function?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A function can call multiple methods in it. When an error occurs in one of the methods, you will need to reverse the methods called before the error. Reverting the output of the methods is done by implementing a rollback function and calling it every time an error occurs.&lt;/p&gt;

&lt;p&gt;The concept of the rollback function is reverse-engineering. First, extract information from the called methods. This information is the success status of each method. The state may be a boolean variable with a value equal to true when the method succeeds and false when it fails; it also may be a string with the result of the method.&lt;/p&gt;

&lt;p&gt;Then, collect the information and abstract it into a model. Finally, pass this model to the rollback function and review each status; if it is a success status, reverse the method and if it is a failure status, ignore it.&lt;/p&gt;

&lt;p&gt;In this way, there are no side effects to the function when an error occurs. Moreover, the program will be maintainable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Modeling
type CreateHotelStatusModel struct {
    HotelId             string `json:"hotelId"`
    FloorId             string `json:"floorId"`
    RoomId              string `json:"roomId"`
    IsFirstFloorCreated bool   `json:"isFirstFloorCreated"`
    IsFirstRoomCreated  bool   `json:"isFirstRoomCreated"`
}

func CreateHotel(hotelName string) error {
    var hotelStatusModel CreateHotelStatusModel
    //create hotel DB record
    hotelId, createHotelDBErr := CreateHotelDBRecord(hotelName)
    //Information extraction
    hotelStatusModel.HotelId = hotelId
    if createHotelDBErr != nil {
        createHotelRollback(&amp;amp;hotelStatusModel)
        return createHotelDBErr
    }

    //create first floor
    floorId, isFloorCreated, createFloorErr := CreateFirstFloor(hotelId)
    hotelStatusModel.FloorId = floorId
    hotelStatusModel.IsFirstFloorCreated = isFloorCreated
    if createFloorErr != nil {
        createHotelRollback(&amp;amp;hotelStatusModel)
        return createFloorErr
    }

    //create first room
    roomId, isRoomCreated, createRoomErr := CreateFirstRoom(floorId)
    hotelStatusModel.RoomId = roomId
    hotelStatusModel.IsFirstRoomCreated = isRoomCreated
    if createRoomErr != nil {
        createHotelRollback(&amp;amp;hotelStatusModel)
        return createRoomErr
    }
    DispatchEvent("success","topic")
    return nil
}

//Review
func createHotelRollback(statusModel *CreateHotelStatusModel) {
    if statusModel.HotelId != "" {
        DeleteHotelDBRecord(statusModel.HotelId)
    }
    if statusModel.IsFirstFloorCreated {
        DeleteFloor(statusModel.FloorId)
    }
    if statusModel.IsFirstRoomCreated {
        DeleteRoom(statusModel.RoomId)
    }
    DispatchEvent("failure","topic")
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>go</category>
      <category>backend</category>
      <category>rollbackfunctions</category>
      <category>errorshandling</category>
    </item>
  </channel>
</rss>
