你应该如何编写这个函数以返回一个指针?

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

How should I write this function to return a pointer?

问题

假设我们有以下结构体:

type BuyerData struct {
    id int
    balance float64
}
type AgentData struct {
    id int
    buyerData BuyerData
}
type GlobalData struct {
   agents map[int]AgentData
}

如果我想为GlobalData定义一个类似于Java中的类方法的"insert"方法,以返回给定id上的buyerData的指针,该方法的签名如下:

func (globalData *GlobalData) getBuyerData(id int) *BuyerData

在这个方法中应该写什么?我遇到了各种错误,比如cannot take the address ofinvalid indirect ofdoes not support indexing...


这是我目前写的代码,它不会产生任何编译异常:

func (globalData *GlobalData) getBuyerData(id int) *BuyerData {
    var agents *map[int]AgentData = &(globalData.agents)
    var agentData AgentData = (*agents)[id]
    return &(agentData.buyerData)
}

但是,老实说,我不知道自己在做什么,也不知道这是否是正确的方法。

英文:

Assuming we have the following structures:

type BuyerData struct {
    id int
    balance float64
}
type AgentData struct {
    id int
    buyerData BuyerData
}
type GlobalData struct {
   agents map[int]AgentData
}

if I wanted to define a "insert equivalent of Java's class method" for GlobalData to return a pointer to it's value buyerData on a given id, which would be a method with the following signature:

func (globalData *GlobalData) getBuyerData(id int) *BuyerData

What would be written inside it? I'm having troubles because it's giving all sorts of errors like cannot take the address of, invalid indirect of or does not support indexing...


This is what I currently have that does not generate any compiler exception:

func (globalData *GlobalData) getBuyerData(id int) *BuyerData {
	var agents *map[int]AgentData = &(globalData.agents)
	var agentData AgentData = (*agents)[id]
	return &(agentData.buyerData)
}

But I, honestly, don't know what I'm doing or if this is the correct way of doing it.

答案1

得分: 3

假设你只想返回一个指向在AgentData结构体中由函数参数索引的map中的值的指针,你可能想要这样写:

func (agentsGlobalData *AgentData) getBuyerData(id int) (*BuyerData, bool) {
    buyer, has := agentsGlobalData.agents[id]
    return &buyer, has
}

你也可以考虑将指向AgentData的指针存储在该map中,使你的结构体如下:

type GlobalData struct {
   agents map[int]*AgentData
}

但是你没有告诉我们足够的关于你的用例,以便推荐其中的一种方法。

(你说调用者应该能够“写入”BuyerData,但是该结构体中没有任何公共字段...)

英文:

Assuming you'd just like to return a pointer to the value in the map in a AgentData struct indexed by the argument of the function, you likely mean to say:

func (agentsGlobalData *AgentData) getBuyerData(id int) (*BuyerData, bool) {
    buyer, has := agentsGlobalData.agents[id]
    return &buyer, has
}

You might also consider storing pointers to AgentData in that map instead, making your struct:

type GlobalData struct {
   agents map[int]*AgentData
}

But you haven't told us enough about your use case to recommend one or the other, perhaps.

(You say callers should be able to "write" to a BuyerData, but there are not any public fields in that struct...)

答案2

得分: 0

你需要这样说:

func (globalData *GlobalData) GetBuyerData(id int) *BuyerData {
  if agent, found := globalData.Agents[id]; found {
    return &agent.BuyerData
  }
  return nil
}

在这里查看:https://play.golang.org/p/lwXn4D1mr3J

英文:

You need to say something like this:

func (globalData *GlobalData) GetBuyerData(id int) *BuyerData {
  if agent, found := globalData.Agents[id]; found {
    return &agent.BuyerData
  }
  return nil
}

See it here: https://play.golang.org/p/lwXn4D1mr3J

huangapple
  • 本文由 发表于 2021年6月22日 05:08:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/68074741.html
匿名

发表评论

匿名网友

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

确定