如何在 Go 中使用接口(interfaces)在 maps 和 slices 中?

huangapple go评论75阅读模式
英文:

Go : How to use interfaces in maps and slices

问题

我需要一个接口的映射,因为我想能够运行一个函数,该函数可以使用接口的具体实现,而不用关心这些结构体可以做的“额外”事情。

我已经阅读了https://jordanorelli.com/post/32665860244/how-to-use-interfaces-in-go,其中对指针和接口有一个非常好的解释,但我仍然不知道如何在实践中实现我想要的功能。

我正在尝试下面的代码:

https://play.golang.com/p/nRH2IyK7t9F

package main

import (
	"fmt"
)

type IExample interface {
	GetName() string
}

type Concrete struct {
	Name string
}

func (c *Concrete) GetName() string {
	return c.Name
}

func main() {
    // 在我的真实应用程序中,这是从 Gorm orm 返回的切片
	var s = []Concrete{
		Concrete{Name: "one"},
		Concrete{Name: "two"},
	}

	foo := make(map[string]IExample)
	bar := []IExample{}

	for _, c := range s {
		foo[c.Name] = &c
		bar = append(bar, &c)
		fmt.Printf("Set key [%s]\r\n", c.Name)
	}

	for name, element := range foo {
		fmt.Printf("key: [%s] has element [%s]\r\n", name, element.GetName())
	}
	for name, element := range bar {
		fmt.Printf("key: [%d] has element [%s]\r\n", name, element.GetName())
	}

}

这将输出:

Set key [one]
Set key [two]
key: [one] has element [two]
key: [two] has element [two]
key: [0] has element [two]
key: [1] has element [two]

我真正想要的是将元素 one 放在键 "one" 中。

我想问题发生在使用引用 foo[c.Name] = &c 进行赋值时。我需要这样做,否则我会得到错误 "cannot use c (variable of type Concrete) as IExample value in assignment: Concrete does not implement IExample (method GetName has pointer receiver)"。

在阅读了https://dusted.codes/using-go-generics-to-pass-struct-slices-for-interface-slices之后,我想知道是否可以使用泛型来解决这个问题,但我无法弄清楚如何实现。

type ExampleMap map[string]IExample

func (e *ExampleMap) add[T IExample](id string, item T) {
	e[id] = item
}
// 语法错误:方法不能有类型参数

我该如何使这个映射包含正确的元素?

英文:

I need a map of an interface because I want to be able to run a function that can use concrete implementations of either interface without caring about the "extra" stuff that those structs can do.

I've read https://jordanorelli.com/post/32665860244/how-to-use-interfaces-in-go which has a really great explanation of pointers and interfaces, but I still don't know how to accomplish what I want in practice.

I am trying the below code:

https://play.golang.com/p/nRH2IyK7t9F

package main

import (
	"fmt"
)

type IExample interface {
	GetName() string
}

type Concrete struct {
	Name string
}

func (c *Concrete) GetName() string {
	return c.Name
}

func main() {
    // in my real application this is a slice returned from Gorm orm
	var s = []Concrete{
		Concrete{Name: "one"},
		Concrete{Name: "two"},
	}

	foo := make(map[string]IExample)
	bar := []IExample{}

	for _, c := range s {
		foo[c.Name] = &c
		bar = append(bar, &c)
		fmt.Printf("Set key [%s]\r\n", c.Name)
	}

	for name, element := range foo {
		fmt.Printf("key: [%s] has element [%s]\r\n", name, element.GetName())
	}
	for name, element := range bar {
		fmt.Printf("key: [%d] has element [%s]\r\n", name, element.GetName())
	}

}

This outputs:

Set key [one]
Set key [two]
key: [one] has element [two]
key: [two] has element [two]
key: [0] has element [two]
key: [1] has element [two]

What I really want is for element one to be in key "one".

I imagine that the problem is happening because of the assignment using a reference foo[c.Name] = &c. I need this because otherwise I get the error "cannot use c (variable of type Concrete) as IExample value in assignment: Concrete does not implement IExample (method GetName has pointer receiver)"

After reading https://dusted.codes/using-go-generics-to-pass-struct-slices-for-interface-slices I wondered if the problem could be solved using generics, but I can't work out how to implement that.

type ExampleMap map[string]IExample

func (e *ExampleMap) add[T IExample](id string item T) {
	e[id] = item
}
// syntax error: method must have no type parameters

How can I get this map to contain the correct elements?

答案1

得分: 2

你所做的是正确的。你只是将错误的东西放入了map中:

for i, c := range s {
    foo[c.Name] = &s[i]
    bar = append(bar, &s[i])
    fmt.Printf("Set key [%s]\r\n", c.Name)
}

循环变量在每次迭代时被重写,所以当你将&c添加到map和slice中时,你添加的指针是c的地址,而c在每次迭代时都被重写。

英文:

What you are doing is correct. You just put the wrong thing into the map:

for i,c := range s {
        foo[c.Name] = &s[i]
        bar = append(bar, &s[i])
        fmt.Printf("Set key [%s]\r\n", c.Name)
    }

The loop variable is rewritten at each iteration, so when you add &c to the map and to the slice, the pointer you add is the address of c, which is overwritten at each iteration.

huangapple
  • 本文由 发表于 2023年3月16日 01:46:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75748370.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定