如何访问 Golang 中的接口字段?

有两个结构和一个接口。一种结构用于 gfg 课程细节,另一种结构用于竞赛细节。一个界面是 get_name 方法,它将返回课程和竞赛的名称。在接口的帮助下,我们将访问结构的变量,因为我们不想从外部访问结构的变量。

示例 1: 该程序将接受 2 个输入。

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))
}

输出:

Enter course name: DBMS
Enter contest name: DBMS
Is same subjects in course and contest: true

示例 2: 该程序将接受 2 个输入。

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))
}

输出:

Enter course price: 1000
Enter Coupon Price: 700
Is the course is free: false