- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
- Related Articles
- Golang Program to Show Encapsulation in Class
- Golang Program to Show Inheritance in Class
- Golang program to show overloading of methods in class
- Golang Program to Show Usage of Static keyword in Class
- Golang program to show use of super keyword in class
- Data Hiding in Python
- Golang Program to Create Abstract Class
- Golang program to show overriding of methods in classes
- C++ program to hold student info using data hiding and encapsulation
- Golang Program to Create a Simple Class?
- Golang Program to Create an enum class
- Golang Program to Show Usage of Access Modifier
- Golang Program to Create a Class and Object
- Golang Program to Print object of a class
- Swift Program to Show Overloading of Methods in Class
