Home > Backend Development > Golang > How Can I Use Custom Interfaces with Go 1.8 Plugins?

How Can I Use Custom Interfaces with Go 1.8 Plugins?

Susan Sarandon
Release: 2024-12-27 19:02:09
Original
264 people have browsed it

How Can I Use Custom Interfaces with Go 1.8 Plugins?

Using Custom Interfaces in Go 1.8 Plugins

In Go 1.8, plugins can use custom interfaces. However, there are some limitations to consider.

Using an External Package

One approach is to define the interface in an external package and have both the plugin and the main application import it.

For example, create an interface in a package called filter:

package filter

type Filter interface {
    Name() string
    Age() int
}
Copy after login

In the plugin, import the filter package and implement the interface:

package main

import (
    "fmt"
    "filter"
)

type plgFilter struct{}

func (plgFilter) Name() string { return "Bob" }
func (plgFilter) Age() int     { return 23 }

func GetFilter() (f filter.Filter, err error) {
    f = plgFilter{}
    fmt.Printf("[plugin GetFilter] Returning filter: %T %v\n", f, f)
    return
}
Copy after login

In the main application, import the filter package and load the plugin:

package main

import (
    "fmt"
    "filter"
    "plugin"
)

func main() {
    p, err := plugin.Open("pg/pg.so")
    if err != nil {
        panic(err)
    }

    GetFilter, err := p.Lookup("GetFilter")
    if err != nil {
        panic(err)
    }
    filter, err := GetFilter.(func() (filter.Filter, error))()
    fmt.Printf("GetFilter result: %T %v %v\n", filter, filter, err)
    fmt.Println("\tName:", filter.Name())
    fmt.Println("\tAge:", filter.Age())
}
Copy after login

This approach ensures that the plugin has access to the interface definition.

Returning Interface{}

Another option is to have the plugin return a value of type interface{}. The main application can then define the interface it expects and use type assertion on the returned value.

For example, in the plugin:

package main

import (
    "fmt"
)

type plgFilter struct{}

func (plgFilter) Name() string { return "Bob" }
func (plgFilter) Age() int     { return 23 }

func GetFilterIface() (f interface{}, err error) {
    f = plgFilter{}
    fmt.Printf("[plugin GetFilterIface] Returning filter: %T %v\n", f, f)
    return
}
Copy after login

In the main application:

package main

import (
    "fmt"
    "plugin"
)

func main() {
    p, err := plugin.Open("pg/pg.so")
    if err != nil {
        panic(err)
    }

    GetFilterIface, err := p.Lookup("GetFilterIface")
    if err != nil {
        panic(err)
    }
    filterIface, err := GetFilterIface.(func() (interface{}, error))()
    fmt.Printf("GetFilterIface result: %T %v %v\n", filterIface, filterIface, err)
    myfilter := filterIface.(MyFilter)
    fmt.Println("\tName:", myfilter.Name())
    fmt.Println("\tAge:", myfilter.Age())
}

type MyFilter interface {
    Name() string
    Age() int
}
Copy after login

This approach provides more flexibility, but requires type assertion in the main application.

Limitations

Note that custom interfaces work only if the interface is defined outside of the plugin. This is because Go plugins are self-contained modules that cannot access types defined in other packages.

The above is the detailed content of How Can I Use Custom Interfaces with Go 1.8 Plugins?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template