Golang Program to Show Encapsulation in Class


In this article, we are going to learn about Encapsulation in class using Golang Program

Encapsulation in go Language vs Other Object-Oriented Languages − The variables or data of a class in object-oriented languages are private to that class and can only be accessed through any member functions of the class in which they are specified. However, classes and objects are not supported by the Go language. So using packages to accomplish encapsulation in the Go programming language. Go offers exported and unexported identifiers, two separate forms of identifiers. Exporting variables, functions, methods, fields, and structures from the packages enables encapsulation and helps manage the elements' visibility (variables, functions, methods, field, structures). If the package in which they are specified is present in your program, the items are visible.

  • Exported Identifiers in go programming language − Identifiers that are exported from the package in which they are specified are known as exported identifiers. These identifiers always begin with a capital letter. The capital letter denotes an exported identifier, which is what the given identifier is. Identifiers that are exported are always only valid within the package in which they are defined. You simply export the name of the provided identifier, not its implementation, when you export it from the package. Additionally, this methodology can be used with fields, methods, and structures.

  • Unexported Identifiers in go programming language − Identifiers that are not exported from any package are referred to as unexported identifiers. All of them are lowercase. The addition function is not associated to any package, as seen in the example below, hence it is not an exported function, and its visibility is solely available to this application.

Example 1

Encapsulation in go Programming Language Using Exported Indentifiers −

Let us now consider an example in which we will try to convert an array of strings to upper case through the concept of encapsulation using exported functions.

package main

import (
   "fmt"
   "strings"
)

// fmt package allows us to print anything on the screen.
// strings package allows us to use other predefined functions like ToUpper()

// calling the main function
func main() {

   // creating an array of strings and assigning values to it
   arr := []string{"apple", "banana", "fruits"}

   // converting the letters of the string declared above to uppercase using ToUpper()
   // method defined in strings package.
   fmt.Println("Successfully converted array of strings to upper case using Exported method ToUpper() defined in strings package")
   fmt.Println("The resultant string is:")
   for x := 0; x < len(arr); x++ {

      // calling the exported method ToUpper()
      // storing the result in a new array called results
      results := strings.ToUpper(arr[x])

      // printing the result on the screen
      fmt.Println(results)
   }
}

Output

Successfully converted array of strings to upper case using Exported method ToUpper() defined in strings package
The resultant string is:
APPLE
BANANA
FRUITS

Description

  • First, we need to import the required packages like fmt and strings. fmt package allows us to print anything on the screen and strings package allows us to use other predefined methods defined in it like ToUpper().

  • Calling the main function. This is the starting point of our program from where it will be started.

  • Initialize an array of strings and store the string values to it.

  • Now start a for loop to index over the array and convert each element of the array to upper case using string.ToUpper() function and store the resultant array in results.

  • Now, print the result on the screen using fmt.Println() function.

Example 2

Encapsulation in go Programming Language Using Unexported Identifiers −

Let us now consider an example in which we will try to find the sum of the array of integers through the concept of encapsulation using an unexported function.

package main
import "fmt"

// fmt package allows us to print anything on the screen
// defining an unexported function addition to find the sum of an array of integers
// this function receives an array of integers as an argument and returns the integer value as the sum
func addition(val []int) int {
   s := 0

   for x := range val {
      s += val[x]
   }
   return s
}

// Calling the main function
func main() {

   // defining an array of integers and storing values in it
   arr := []int{50, 29, 36, 55, 87, 95}

   // calling then unexported method addition() to find the sum of the array and passing the
   // array to it as
   // an argument and storing the result in a separate variable
   result := addition(arr)

   // printing the result on the screen
   fmt.Println("Successfully found the sum of an array of integers using UNExported method addition()")
   fmt.Println("The resultant sum is:")
   fmt.Println(result)
}

Output

Successfully found the sum of an array of integers using UNExported method addition()
The resultant sum is:
352

Description

  • First, we need to import the fmt package. fmt package allows us to print anything on the screen.

  • Initialize and define a method named addition() to find the sum of the array of integers. This function takes an argument as an array of integers and calculates its sum. It then returns the result.

  • Calling the main function. This is the starting point of our program from where it will be started.

  • Initialize an array of integers and store values in it.

  • Now call the addition function by passing the array as an argument to it. Note that while calling the addition function the first letter is in small case this suggests that the function is unexported and is defined in the main itself.

  • Now, store the result in a different variable and print it on the screen.

Updated on: 29-Dec-2022

327 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements