Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何减少go嵌套函数调用中的参数数量_Go - Fatal编程技术网

如何减少go嵌套函数调用中的参数数量

如何减少go嵌套函数调用中的参数数量,go,Go,我有一个go调用树,其结构如下: // state is a common struct shared among all "instances" of MyType - simulating member variables for an Interface (s MyType) Run(state *State){ // called from outside // define goroutines that fetch something via http HTTPCallbac

我有一个go调用树,其结构如下:

// state is a common struct shared among all "instances" of MyType - simulating member variables for an Interface
(s MyType) Run(state *State){ // called from outside
  // define goroutines that fetch something via http
  HTTPCallback(){ // runs on every http response
    parseData(record, outputChan)
  }
}
(s MyType) parseData(rec []string, outputChan chan(interface{})){
  // doesn't need anything from "state" so far
  doIdMapping(string) 
}


doIdMapping(key) { 
   return state.Map[key] 
}
有没有一种方法可以访问Map(它是完全恒定的)而不必通过HTTPCallback传递“state”和所有goroutine,这些goroutine最终都会调用HTTPCallback


这不仅对清晰的代码不好,而且在测试方面也不好。所有中间函数都带有结构指针,它们甚至不需要依赖它。我是否错过了围棋的语言设计/

如果所有这些都在一个包中,您只需在包级别声明
状态
,并在任何地方使用它。比如,

package myHttpClient

import (
    "allthatstuff"
)

state State // not exported but available everywhere in 'myHttpClient'
ExternState State // exported so anyone importing myHttpClient can do myHttpClient.ExternState

如果它是一个“MyType的所有“实例”共享的公共结构”和“完全常量”,听起来它可能是一个全局结构,可能是定义这些函数的包的私有结构。如果有不可能的原因,修改这些注释可能有助于澄清问题。您的示例至少缺少所有
func
关键字。理想情况下,示例代码应该编译,但(IMO)至少应该语法正确。非常感谢!我会试试看,然后告诉你我的进展&相应地提高投票率:)