How to Access Interface Fields in Golang?
Last Updated :
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 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
Â
Similar Reads
How to Find Indirect Dependency in Golang? Go, also known as Golang, is a statically typed, compiled language with a rich standard library and a robust ecosystem of packages. When building Go applications, itâs common to use third-party packages to avoid reinventing the wheel. However, these packages can have their own dependencies, known as
4 min read
Embedding Interfaces in Golang In Go language, the interface is a collection of method signatures and it is also a type means you can create a variable of an interface type. As we know that the Go language does not support inheritance, but the Go interface fully supports embedding. In embedding, an interface can embed other inter
6 min read
How to Find Length of Struct in Golang? In programming, data structures are vital in organizing and storing data efficiently. One such data structure in Go, also known as Golang, is the struct. This article will delve into the concept of structure in Golang and explore various methods to find its length.Table of ContentWhat is Struct with
5 min read
Interfaces in Golang In Go, an interface is a type that lists methods without providing their code. You canât create an instance of an interface directly, but you can make a variable of the interface type to store any value that has the needed methods.Exampletype Shape interface { Area() float64 Perimeter() float64}In t
3 min read
reflect.Interface() Function in Golang with Examples Go language provides inbuilt support implementation of run-time reflection and allowing a program to manipulate objects with arbitrary types with the help of reflect package. The reflect.Interface() Function in Golang is used to get the v's current value as an interface{}. To access this function, o
2 min read