每个被调用的函数都可以有一个函数ID,并且可以与其进行通信吗?

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

Can I have function ID for each function being called and communicate with it?

问题

我将调用一个函数来使用go functionABC()建立一个BOSH连接。

在这个函数中,我将通过发送ping信息来保持连接的状态。所以可能会有很多functionABC()的调用。现在,如果我想从这个函数中获取一些信息,我能否通过函数ID或进程ID来标识这个函数?Go语言中是否有函数ID或进程ID来标识一个函数?

如果有的话,我该如何与这个函数进行通信?如果没有,是否存在其他的替代方法来实现这个目标?

英文:

I will call a function to build a BOSH connection using go functionABC().

In the function, I will keep the status of the connection by sending ping information. So, there may be a lot of functionABC() calls. So now, if I want to get some information from the function, can I have some way to identify the function by function ID or process ID? Does Go have function ID's or process ID's to identify a function?

If so, how can I communicate with this function? If not, does there exist any alternative way to accomplish it?

答案1

得分: 2

或许可以使用map,从你的函数中返回一个唯一的id/connection,并将其分配给map,类似于这样:

var counter uint64

func ReturnStuff() (uint64, net.Conn) {
    return atomic.AddUint64(&counter, 1), nil
}

var m = map[uint64]net.Conn{}

func main() {
    for i := 0; i < 10; i++ {
        id, conn := ReturnStuff()
        m[id] = conn
    }
    fmt.Printf("%+v", m)
}

这段代码使用了一个counter变量来生成唯一的id,然后将id和nil连接存储在m map中。在main函数中,通过循环调用ReturnStuff函数来生成10个id和连接,并将它们存储在map中。最后,使用fmt.Printf打印出map的内容。

英文:

Maybe use a map and return a unique id/connection from your function and assign it to the map, something like this:

var counter uint64

func ReturnStuff() (uint64, net.Conn) {
	return atomic.AddUint64(&amp;counter, 1), nil
}

var m = map[uint64]net.Conn{}

func main() {
	for i := 0; i &lt; 10; i++ {
		id, conn := ReturnStuff()
		m[id] = conn
	}
	fmt.Printf(&quot;%+v&quot;, m)
}

huangapple
  • 本文由 发表于 2014年8月5日 00:29:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/25123323.html
匿名

发表评论

匿名网友

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

确定