英文:
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
}
请注意,我已经将代码中的 "
替换为了双引号 "
,以使其成为有效的 Go 代码。
英文:
When looping over a channel, I would like to get an index - to be able to add to an array.
package main
import (
"fmt"
)
func main() {
tasks := []string{"foo", "bar", "baz"}
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) <-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
}
答案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 (
"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) // I would like to add result to an array of results?
// 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
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论