2.42. Go language interface

发布时间 :2023-10-12 23:00:10 UTC      

Go language provides another data type, the interface, which definesall common methods together, and any other type implements this interface as long as they implement these methods.

2.42.1. Example #

/* Define interfaces */
type interface_name interface {
   method_name1 [return_type]
   method_name2 [return_type]
   method_name3 [return_type]
   ...
   method_namen [return_type]
}
/* Define Structure */
type struct_name struct {
   /* variables */
}
/* Implement interface methods */
func (struct_name_variable struct_name) method_name1() [return_type] {
   /* Method implementation */
}
...
func (struct_name_variable struct_name) method_namen() [return_type] {
   /* Method implementation*/
}

2.42.2. Example #

package main
import (
    "fmt"
)
type Phone interface {
    call()
}
type NokiaPhone struct {
}
func (nokiaPhone NokiaPhone) call() {
    fmt.Println("I am Nokia, I can call you!")
}
type IPhone struct {
}
func (iPhone IPhone) call() {
    fmt.Println("I am iPhone, I can call you!")
}
func main() {
    var phone Phone
    phone = new(NokiaPhone)
    phone.call()
    phone = new(IPhone)
    phone.call()
}

In the above example, we define an interface Phone , there is a method inthe interface call() . And then we’re here. main function definesa Phone type variable and assign it a value of NokiaPhone and IPhone . And then call call() method, and the output is as follows:

I am Nokia, I can call you!
I am iPhone, I can call you!

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.