Golang Program to Show Usage of Access Modifier


In go programming language, there are two types of access modifiers i.e Exported and un-exported. Identifiers that are exported from the package in which they are specified are known as exported identifiers. They always begin with a capital letter. These are only valid in the package in which they are defined. Identifiers that are not exported from any package are referred to as un-exported identifiers that are written in lowercase.

Syntax

func len(v Type) int

The len() function is used to get the length of a any parameter. It takes one parameter as the data type variable whose length we wish to find and returns the integer value which is the length of the variable.

func ToUpper(str string) string

ToUpper() function is present in strings package and is used to convert a given string to uppercase. The function accepts the given string as argument and returns the final string after converting it to upper case.

Method 1: 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.

Algorithm

  • Step 1 − First, we need to import the required packages like fmt and strings.

  • Step 2 − Then, call the main() function.

  • Step 3 − Initialize an array of strings and store the string values to it. further, print the array on the screen.

  • Step 4 − 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.

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

Example

The following example will explain how to access modifiers in go programming language using exported identifiers

package main
import (
   "fmt"
   "strings"
)
func main() {

   // creating an array of strings and assigning values to it
   arr := []string{"apple", "banana", "fruits"}
   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()
      results := strings.ToUpper(arr[x])
      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

Method 2: Using un-exported indentifiers

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 un-exported function.

Algorithm

  • Step 1 − First, we need to import the fmt package.

  • Step 2 − 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.

  • Step 3 − It uses for loop to iterate over each element of the array and find its sum and stores it in a new declared variable called s. The function then returns the result.

  • Step 4 − Now, call the main() function.

  • Step 5 − Initialize an array of integers and store values to it. further print the array on the screen using fmt.Println() function.

  • Step 6 − 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 main itself.

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

Example

The following example will explain how to access modifiers in go programming language using unexported identifiers

package main
import "fmt"
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}
   fmt.Println("The given array of integers is:", arr)
   result := addition(arr)
   fmt.Println()
   fmt.Println("Succesfully found the sum of array of integers using UnExported method addition()")
   fmt.Println("The resultant sum is:")
   fmt.Println(result)
}

Output

The given array of integers is: [50 29 36 55 87 95]

Succesfully found the sum of array of integers using UnExported method addition()
The resultant sum is:
352

Conclusion

We have successfully compiled and executed a go language program to show access modifiers along with examples. Here we have used the two types of access modifiers in go programming language that are exported and unexported.

Updated on: 16-Feb-2023

730 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements