How to Access Interface Fields in Golang? Last Updated : 31 Aug, 2021 Comments Improve Suggest changes Like Article Like 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  Comment More infoAdvertise with us Next Article How to Access Interface Fields in Golang? C cse1604310056 Follow Improve Article Tags : Go Language Golang-Interfaces Golang-Program 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 Get Current time in Golang? With the help of time.Now() function, we can get the current time in Golang by importing time module. Syntax: time.Now() Return: Return current date and time. Example #1: In this example, we can see that by using time.Now() function, we are able to get the current date and time. C // Golang program 1 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 How to find the type of Struct in Golang? A structure or struct in Golang is a user-defined data type which is a composition of various data fields. Each data field has its own data type, which can be a built-in or another user-defined type. Struct represents any real-world entity that has some set of properties/fields. Go does not support 3 min read How to Create Modules in Golang? Go has included support for versioned modules as proposed here since 1.11 with the initial prototype as vgo. The concept of modules in Go was introduced to handle the problem of dependencies within your Go applications. Several packages are collected and combined to form a module which is stored in 3 min read Multiple 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. In Go language, you are allowed to create multiple interfaces in your program with the help of the given syntax: type interface_name interface{ // Method sig 4 min read How to fetch an Integer variable as String in GoLang? To fetch an Integer variable as String, Go provides strconv package which has methods that return a string representation of int variable. There is nothing like an integer variable is converted to a string variable, rather, you have to store integer value as a string in a string variable. Following 2 min read Like