为什么在调用函数之前和函数内部,我的go函数参数的大小不同?

时间:2020-06-15 15:40:36

标签: go

作为Golang测试的一部分,我最终遇到了障碍,所以我是新手。

我编写了一个快速的玩具程序来搜索字符串列表中的模式,我想同时进行。

我编写了一个search(data *[]string, input_string *string, channel chan<- []string)函数,通过一个通道将过滤后的data发送回结果。

但是,在我将data传递给函数之前,search的大小似乎有所不同:在var channels [](chan []string) for _, chunk := range chunks { channel := make(chan []string, 1) channels = append(channels, channel) go search(&chunk, &input_string, channel) } var filtered_data []string for _, channel := range channels { part_filtered := <-channel filtered_data = append(filtered_data, part_filtered...) } 内部,它的大小非常小,只是不正确。

完整的代码在这里:https://gitlab.com/jiehong/kata_test_language/-/blob/master/wiki.go#L39,因此您可以对其进行测试。

基本上,我启动了一个go例程列表,并从另一个通道检索它们的结果:

chunk

但是search尽管在循环内有效,但在public class Demo { public Demo(ConsiliIntContext context) { } } 处理时似乎为空。

我在做什么错了?

1 个答案:

答案 0 :(得分:0)

问题是search需要一个*[]string。将其更改为[]string即可解决。

Volker将此表示为“也”,但已解决。

如hmm所指出的那样,它可能链接到https://github.com/golang/go/wiki/CommonMistakes#using-goroutines-on-loop-iterator-variables

正在运行go vet wiki.go并没有警告此问题。