循环通道,但缺少索引。

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

go looping channels, but missing an index

问题

当在通道上进行循环时,我想要获取一个索引,以便能够添加到一个数组中。

package main

import (
  "fmt"
)

func main() {
  tasks := []string{"foo", "bar", "baz"}
  results := process(tasks)

  for index, result := range results {
    fmt.Println(result) // 我想将 result 添加到一个结果数组中?
    // newresults[index] = result???
  }
}

func process(tasks []string) <-chan string {
  ch := make(chan string)
  go func() {
    for index, task := range tasks {
      ch <- fmt.Sprintf("processed task %d: %s", index, task)
    }
    close(ch)
  }()
  return ch
}

请注意,我已经将代码中的 &quot; 替换为了双引号 ",以使其成为有效的 Go 代码。

英文:

When looping over a channel, I would like to get an index - to be able to add to an array.

package main

import (
  &quot;fmt&quot;
)

func main() {
  tasks := []string{&quot;foo&quot;, &quot;bar&quot;, &quot;baz&quot;}
  results := process(tasks)

  for result := range results { // index?
    fmt.Println(result) // I would like to add result to an array of results?
    // newresults[index] = result???
  }
}

func process(tasks []string) &lt;-chan string {
  ch := make(chan string)
  go func() {
    for index, task := range tasks {
      ch &lt;- fmt.Sprintf(&quot;processed task %d: %s&quot;, index, task)
    }
    close(ch)
  }()
  return ch
}

答案1

得分: 2

例如,

i := 0
for result := range results {
    fmt.Println(result)
    newresults[i] = result
    i++
}
英文:

For example,

i := 0
for result := range results {
    fmt.Println(result)
    newresults[i] = result
    i++
}

答案2

得分: 2

作为对peterSO答案的另一种选择,你可以简单地使用append将内容添加到切片的末尾。

英文:

Aternatively to peterSO's answer, you can simply use append to add to the end of your slice.

答案3

得分: 1

通道(Channels)没有索引。如果你想要追踪计数,可以在 for 循环内创建自己的计数变量并进行递增。

另一种方法是创建一个带有索引和任务名称的结构体。

package main

import (
	"fmt"
)

type Task struct {
	Index int
	Task  string
}

func main() {
	tasks := []string{"foo", "bar", "baz"}
	results := process(tasks)
	myresults := make([]*Task, 3)

	for result := range results { // index?
		fmt.Println(result) // 我想将 result 添加到结果数组中?
		// results[index] = result???
		myresults[result.Index] = result
	}
}

func process(tasks []string) <-chan *Task {
	ch := make(chan *Task)
	go func() {
		for index, task := range tasks {
			t := &Task{Index: index, Task: task}
			ch <- t
			// ch <- fmt.Sprintf("processed task %d: %s", index, task)
		}
		close(ch)
	}()
	return ch
}

希望对你有帮助!

英文:

Channels do not have an index. If you want to track the count, create your own count variable and increments within the for loop.

The alternative is to create a struct with a index and the task name.

package main

import (
	&quot;fmt&quot;
)

type Task struct {
	Index int
	Task  string
}

func main() {
	tasks := []string{&quot;foo&quot;, &quot;bar&quot;, &quot;baz&quot;}
	results := process(tasks)
	myresults := make([]*Task, 3)

	for result := range results { // index?
		fmt.Println(result) // I would like to add result to an array of results?
		// results[index] = result???
		myresults[result.Index] = result
	}
}

func process(tasks []string) &lt;-chan *Task {
	ch := make(chan *Task)
	go func() {
		for index, task := range tasks {
			t := &amp;Task{Index: index, Task: task}
			ch &lt;- t
			// ch &lt;- fmt.Sprintf(&quot;processed task %d: %s&quot;, index, task)
		}
		close(ch)
	}()
	return ch
}

huangapple
  • 本文由 发表于 2016年2月5日 00:55:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/35206953.html
匿名

发表评论

匿名网友

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

确定