Open In App

How to Access Interface Fields in Golang?

Improve
Improve
Like Article
Like
Save
Share
Report

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 fields
package main
 
import "fmt"
 
// Declare course structure
type Course struct {
    name string
}
 
// Declare contest structure
type Contest struct {
    name string
}
 
// Declare interface
type Name interface {
    get_name() string
}
 
// get_name function for course
func (a Course) get_name() string {
 
    return a.name
 
}
 
// get_name function for contest
func (b Contest) get_name() string {
 
    return b.name
 
}
 
// Compare course and contest name.
// Name is interface type
func 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 fields
package main
 
import "fmt"
 
// Declare courseprice structure
type Courseprice struct {
    price int
}
 
// Declare contestprice structure
type Couponprice struct {
    price int
}
 
// Declare interface
type Price interface {
    get_price() int
}
 
// get_price function for Courseprice
func (a Courseprice) get_price() int {
 
    return a.price
 
}
 
// get_price function for Coupon price
func (b Couponprice) get_price() int {
 
    return b.price
 
}
 
// Compare courseprice and Couponprice.
// Price is interface type
func 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

 



Last Updated : 31 Aug, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads