Facebook Twitter Instagram
    Thursday, June 30
    Trending
    • Google Password Manager Gets Improved Across The Board
    • 2000-HP Drako Dragon Electric SUV Revealed, Showing Bold Looks
    • Monkey Island Creator Deserves Better Than What Toxic Gamers Just Displayed
    • The Fed’s Preferred Inflation Gauge Moderated Slightly in May.
    • Trading for BTT, DENT, KEY and UNFI Starts July 1 – Deposit Now!
    • Reporting ‘limited progress,’ FATF urges countries to introduce legislation for travel rule
    • Great Outfits in Fashion History: Brandy in a Lustrous Light-Green Suit
    • Ketanji Brown Jackson swearing-in: New Supreme Court justice is sworn in as Stephen Breyer retires
    Facebook Twitter Instagram Pinterest VKontakte
    Swave Digest
    • Home
    • World News
    • Technology
      • Smartphones
      • Computers
      • Programming
      • Automobiles
    • Entertainment
      • Music
      • Anime
      • Movies
    • Sports
      • Football
      • Basketball
      • Tennis
    • Business
      • Crypto
      • Stocks
      • NFT
    • Lifestyle
      • Fashion
      • Health
      • Travel
    • Shop
    Swave Digest
    Home»Technology»Programming»How to Achieve Principles of OOPs in Go | by Nikhil Vaidyar | Jun, 2022
    Programming

    How to Achieve Principles of OOPs in Go | by Nikhil Vaidyar | Jun, 2022

    Swave DigestBy Swave DigestJune 16, 2022No Comments5 Mins Read
    Facebook Twitter Pinterest LinkedIn Tumblr Email
    How to Achieve Principles of OOPs in Go | by Nikhil Vaidyar | Jun, 2022 0*ulDnTOVEnIks4Dtz?resize=1024&w=1024
    Share
    Facebook Twitter LinkedIn Pinterest Email

    Table of Contents

    • Getting familiar with OOPs concept in Go and understanding how Go achieves these principles in its own exceptional way
    • Encapsulation
    • Inheritance
    • Polymorphism
    • Enums

    Getting familiar with OOPs concept in Go and understanding how Go achieves these principles in its own exceptional way

    How to Achieve Principles of OOPs in Go | by Nikhil Vaidyar | Jun, 2022 4415 829434How to Achieve Principles of OOPs in Go | by Nikhil Vaidyar | Jun, 2022 4415
    Red moon in darkness of universe
    Photo by Federico Beccari on Unsplash

    You can see above that there is an alluring red heavenly body that looks and describes a lot more than this picture in this darkness of the universe with millions of stars and planets away from it. Well, we can also relate the same to our topic today like the universe is a big class of objects means the sun, planets, moons, asteroids, etc, are all objects of that class. In fact, the moonlight also plays a role in inheritance the moonlight which is actually dependent on the sun’s light. Hence, we can relate OOP’s concepts to a lot of things. So, let’s get a bit more familiar in respect with Go.

    OOPs stands for Object-Oriented Programming Systems and are the type of programming in which we need to define the data structure for the data type and the certain operation to modularize our program structure. For more, you can refer to this link: https://en.wikipedia.org/wiki/Object-oriented_programming

    We need OOPs when we need to modularize our complex code structure, make it more reliable, and maintainable, provide a good framework for code libraries, etc.

    Let’s consider Go to be a light object-oriented programming language because of Go's simplicity. Go use the same alike concepts of OOPs but not all in one go. First of all, Go is not an OOP language and it is not designed to be like that because the principles of OOPs are designed to be consists of class and object structure to maintain the code infrastructure via implementing super class, abstract class, constructors, objects, derived class, and destructors, etc. Well, there is a lot to that. Talking about Go, it just implements its OOPs structure in its own way. Well, this debate can go long, but here’s an official FAQ answer available on this link: https://go.dev/doc/faq#Is_Go_an_object-oriented_language. Go check it out.

    Here are some concepts of OOPs below with respect to Go, so let’s try to get familiar with them.

    Encapsulation

    How to Achieve Principles of OOPs in Go | by Nikhil Vaidyar | Jun, 2022 0*LrOvPSXUBcPkDO9n
    Photo by Dayne Topkin on Unsplash

    In simpler terms encapsulation means if we have single entity data that is written inside any class or package it is hidden for another class or for the outside world whereas abstraction means to hide unnecessary information

    In Go, we use this principle at package level because it doesn’t provide access modifiers like private, public, and protected. It also plays a role in variables, structs, methods, and functions, so if you say, for example, declare a function with the capital letter that function could be easily invoked in the current package as well as in another package hence it makes it publicly available in code structure whereas if you declare a function with a small letter it would only be available to that particular package, hence making it more private and a bit secure.

    Data hiding in Go

    Hence, from the above example, you can see how functions can become private and public according to case sensitivity.

    In terms of OOPs, encapsulation and abstraction are more than just data hiding and access specification it bring more features to the classes and interfaces, but in the case of Go, it just uses the lighter version of both concepts.

    Inheritance

    How to Achieve Principles of OOPs in Go | by Nikhil Vaidyar | Jun, 2022 1*IG4MvVVrjeko 8 FN0 ZqA

    In brief, inheritance means to create new classes(subclasses or derived classes) from existing base(parent) classes and achieve code reusability. In Go, there is no such thing as inheritance but there is composition to achieve structure inheritance via struct-embedding means to embed fields of one struct to another struct. You can get to know more about struct-embedding by this link: https://gobyexample.com/struct-embedding

    Composition in Go

    Go only supports composition if we say it in terms of inheritance. While inheritance is more than just composition as people who are having experience in OOPs languages like JAVA, C++, etc.

    Polymorphism

    Polymorphism is the oop concept in which an object is capable of molding into multiple forms just like three spiderman’s in our case from a different dimension with different forms but the entity is the same.

    In Go, we can achieve polymorphism via implementing interfaces in Go. Interfaces work as a collection of methods. Let’s get more familiar with an example.

    Polymorphism in Go

    Hence, in the above example, as you can see the Developer struct implements the Tasks interface by defining methods like BugFix and Improvement. I have written this example because we developers are much more friendly with these terms in our days while getting assigned different versions of tasks like bugfix, improvements, hotfix, etc.

    Enums

    Enum is a special data type with enumerated data which consists of a set of elements. Mostly the enumerated data is of constant type in many languages.
    In the case of Go, we can create enums and define constant variables accordingly by declaring iota to the first variable in a series of it. The iota keyword represents the integer constant from zero.

    const (
    Java = iota
    Go
    Javascript
    Rust
    )
    Output: 0 1 2 3

    There are some more examples with which we can get to know more about how iota works differently with several ways of initialization.

    const (
    Java = iota
    Go
    _
    Rust
    )
    Output: 0 1 3

    Above, as you can see the _ skips the value in the enum counter

    const (
    Java = iota
    Go
    )
    const (
    Rust = iota
    Javascript
    )
    Output1: 0 1
    Output2: 0 1

    Above, as you can see if you define the different enumerated typed constants they will have their own increment values starting from zero.

    So, that’s it about OOPs in Go. In an upcoming article, I will write that how we can achieve functional programming in Go, and also I will be coming up with system design principles.

    Stay tuned.

    2022 achieve how jun, nikhil oops, principles programming vaidyar
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Swave Digest
    • Website
    • Twitter
    • Pinterest

    Related Posts

    Text-overflow: ellipsis considered harmful | CSS-Tricks

    June 30, 2022

    How to Take an EV Road Trip

    June 30, 2022

    Family travel news for June 2022

    June 30, 2022

    My Favourite Tools for Working With JavaScript | by Tommaso De Ponti | Jun, 2022

    June 30, 2022
    Add A Comment

    Leave A Reply Cancel Reply

    Twitter Instagram Pinterest
    • Home
    • Privacy Policy
    • Terms & Conditions
    • Contact Us
    © 2022 Swave Digest. All Rights Reserved.

    Type above and press Enter to search. Press Esc to cancel.

    Posting....
    We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
    In case of sale of your personal information, you may opt out by using the link Do not sell my personal information.
    Cookie settingsACCEPT
    Manage consent

    Privacy Overview

    This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
    Necessary
    Always Enabled
    Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
    CookieDurationDescription
    cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
    cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
    cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
    cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
    cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
    viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
    Functional
    Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
    Performance
    Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
    Analytics
    Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
    Advertisement
    Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
    Others
    Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
    Save & Accept