How to Access Interface Fields in Golang? - GeeksforGeeks

31 Aug, 2021
Go language interfaces are different from other languages. In Go language, the interface is a custom type that is used to specify a set of one or more method signatures and the interface is abstract, so you are not allowed to create an instance of the interface. But you are allowed to create a variable of an interface type and this variable can be assigned with a concrete type value that has the methods the interface requires. Or in other words, the interface is a collection of methods as well as it is a custom type. To read more about Interfaces, please refer to the article Interface in Golang
There are two structures and one interface. One structure is for gfg course details and another structure is for contest details. One interface is having get_name method which will return names of course and contest. With the help of the interface, we will access the structure’s variable as we don’t want to access the structure’s variable from outside.
Example 1: This program will take 2 inputs.
Go
// Golang program to access the interface fieldspackage main import "fmt" // Declare course structuretype Course struct {    name string} // Declare contest structuretype Contest struct {    name string} // Declare interfacetype Name interface {    get_name() string} // get_name function for coursefunc (a Course) get_name() string {     return a.name } // get_name function for contestfunc (b Contest) get_name() string {     return b.name } // Compare course and contest name.// Name is interface typefunc name_compare(course, contest Name) bool {     return contest.get_name() == course.get_name(); } func main() {     var course_name, contest_name string     // Get the course name from user    fmt.Println("Enter course name: ")    fmt.Scan(&course_name)     // Get the contest's name from user    fmt.Println("Enter contest name: ")    fmt.Scan(&contest_name)     // Create structure of course    course := Course{course_name}     // Create structure of contest    contest := Contest{contest_name}     fmt.Print("Is same subjects in course and contest: ")     // Call interface function to compare names    fmt.Print(name_compare(course, contest))}
Output: 
Enter course name: DBMS
Enter contest name: DBMS
Is same subjects in course and contest: true
Example 2: This program will take 2 inputs.
Go
// Golang program to access the interface fieldspackage main import "fmt" // Declare courseprice structuretype Courseprice struct {    price int} // Declare contestprice structuretype Couponprice struct {    price int} // Declare interfacetype Price interface {    get_price() int} // get_price function for Coursepricefunc (a Courseprice) get_price() int {     return a.price } // get_price function for Coupon pricefunc (b Couponprice) get_price() int {     return b.price } // Compare courseprice and Couponprice.// Price is interface typefunc price_compare(courseprice, Couponprice Price) bool {     if courseprice.get_price() <= Couponprice.get_price() {         return true     } else {         return false     } } func main() {     var courseprice, Couponprice int     // Get the courseprice from user    fmt.Println("Enter course price: ")    fmt.Scan(&courseprice)     // Get the Couponprice  from user    fmt.Println("Enter Coupon Price: ")    fmt.Scan(&Couponprice)     // Create structure of courseprice    course := Courseprice{courseprice}     // Create structure of Couponprice    Coupon := Couponprice{Couponprice}     fmt.Print("Is the course is free: ")     // Call interface function to compare price    fmt.Print(price_compare(course, Coupon))}
Output: 
Enter course price: 1000
Enter Coupon Price: 700
Is the course is free: false
 
RishabhBhatnagar
Golang-Interfaces
Golang-Program
Picked
Go Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
strings.Replace() Function in Golang With Examples
Arrays in Go
How to Split a String in Golang?
Golang Maps
Slices in Golang
Different Ways to Find the Type of Variable in Golang
How to Trim a String in Golang?
How to compare times in Golang?
How to convert a string in lower case in Golang?
Strings in Golang