Explore the conversion of structure to interface in golang

王林
Release: 2024-04-07 11:36:02
Original
864 people have browsed it

There are two methods for structure to interface conversion: embedding the structure or using the adapter pattern. Embedding is a more direct approach that creates a new type with the fields of the structure and the methods of the interface. The Adapter pattern uses an intermediate type that contains instances of the structure and implements the interface. The converted interface only contains interface methods and does not contain other fields of the structure. Both methods can be used to achieve reusability of object-oriented code and provide flexibility in using different interfaces in the system.

Explore the conversion of structure to interface in golang

Conversion from structure to interface in Go language

What are structures and interfaces?

  • Structure is a data type that contains a collection of related data. Each data item is called a field.
  • Interface is an abstract type that defines a collection of methods. Any type that implements these methods can implement the interface.

Conversion from structure to interface

1. Embedding

The simplest way is to embed the structure embedded into the interface. This creates a new type that has both the fields of the structure and the methods of the interface.

type Person struct {
    Name string
    Age  int
}

type Personer interface {
    GetName() string
}

// 嵌入 Person 到 Personer 4
type EmbeddedPerson struct {
    Person
}

func (p EmbeddedPerson) GetName() string {
    return p.Name
}
Copy after login

2. Adapter pattern

Another method is to use the adapter pattern to create a new type that contains an instance of the structure and implements the interface.

type Personer interface {
    GetName() string
}

type Person struct {
    Name string
    Age  int
}

// PersonAdapter 适配器
type PersonAdapter struct {
    p *Person
}

func (a *PersonAdapter) GetName() string {
    return a.p.Name
}

func main() {
    p := Person{"John", 30}
    pa := &PersonAdapter{&p}
    fmt.Println(pa.GetName()) // 输出:John
}
Copy after login

Note:

  • Structure fields must be public so that interface methods can access them.
  • The converted interface type only contains interface methods and does not include other fields of the structure.

Practical case

Suppose we have a User structure that contains name and email. We are going to create an interface Userer so that we can look up users based on their name or email.

Use embedding:

type User struct {
    Name string
    Email string
}

type Userer interface {
    GetName() string
    GetEmail() string
}

type EmbeddedUser struct {
    User
}

func (u EmbeddedUser) GetName() string {
    return u.Name
}

func (u EmbeddedUser) GetEmail() string {
    return u.Email
}
Copy after login

Use adapter mode:

type Userer interface {
    GetName() string
    GetEmail() string
}

type User struct {
    Name  string
    Email string
}

type UserAdapter struct {
    user *User
}

func (ua *UserAdapter) GetName() string {
    return ua.user.Name
}

func (ua *UserAdapter) GetEmail() string {
    return ua.user.Email
}

func main() {
    user := User{"John", "john@example.com"}
    userAdapter := &UserAdapter{&user}
    fmt.Println(userAdapter.GetName())  // 输出:John
    fmt.Println(userAdapter.GetEmail()) // 输出:john@example.com
}
Copy after login

The above is the detailed content of Explore the conversion of structure to interface in golang. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!