Golang program to show data hiding in class


In Golang, data hiding is a practice of preventing external code from accessing or changing a class’s members. This is accomplished by designating the class's members as private, which restricts access to or modification of them to the class's methods only. This is a crucial idea in object-oriented programming since it ensures the data's integrity and the class's proper operation.

Syntax

struct

A struct is a composite data type used in the Go programming language that enables you to bring together related values of various types, such as a collection of fields or a collection of methods. Similar to classes in object-oriented languages, structures can be used to define new types. A point in a 3D space, a user account, or a point in time are just a few examples of the types of data structures that are frequently defined using them. Methods, which are functions connected to a particular struct type and can be used to carry out actions on the data in the struct, are another feature of structures.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) package

  • Step 2 − The next step is to define a struct called "MyStruct" with the properties "private_data" and "public_data". 

  • Step 3 − MyStruct is created as an instance in the main function, and its public method publicmethod is invoked.

  • Step 4 − the public method is called from the main to execute the program.

Example 1

In this example we will see how to show data concealing using structs and methods. The struct has two fields: private_data and public_data in addition to it contains two methods publicmethod and privatemethod.

package main
import "fmt"
type MyStruct struct {
   private_data int // private field
	public_data  int // public field
}
func (m *MyStruct) privatemethod() {      //private method
	fmt.Println("This is a private method")
	fmt.Println("Private data:", m.private_data)
}
func (m *MyStruct) publicmethod() {      //public method
	fmt.Println("This is a public method")
	fmt.Println("Public data:", m.public_data)
	m.privatemethod()  //call privatemethod to print the data
}
func main() {
	myStruct := MyStruct{
		private_data: 42,   //assigning data to private field
		public_data:  27,   // assigning data to public field
	}
	myStruct.publicmethod()
}

Output

This is a public method
Public data: 27
This is a private method
Private data: 42

Example 2

In this example we will see how to show data hiding in class using interfaces.

package main
import "fmt"
type MyInterface interface {
   publicmethod()       //define publicmethod
}
type MyStruct struct {
   private_data int   //create private data
}
func (m *MyStruct) privatemethod() {
   fmt.Println("This is a private method")
   fmt.Println("Private data:", m.private_data)  //print private data
}
func (m *MyStruct) publicmethod() {
   fmt.Println("This is a public method")
   m.privatemethod() //call privatemethod to print the data
}
func main() {
   myStruct := MyStruct{private_data: 42} 
   var myInterface MyInterface = &myStruct
   myInterface.publicmethod()  //call public method
}

Output

This is a public method
This is a private method
Private data: 42

Conclusion

We executed the program of showing data hiding in class using two examples. In the first example we used structs and methods and in the second example we used interfaces along with structs and methods.

Updated on: 01-Feb-2023

202 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements