无法从Go语言中的结构类型通道中获取值。

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

not able to fetch values from struct type channel in Go lang

问题

参考此示例,我想在作业初始化和终止之间添加预定义的延迟。我已经将数据(即jobid和waittime)存储在map中。然后,我将整个map复制到与map相同结构类型的通道中。但是我无法在Go例程调用中获取map的值。请帮助我,我是Go的新手。

package main

import "fmt"

type Vertex struct {
    id, waitime int
}

var m = map[int]Vertex{
    1:  {1, 1000},
    2:  {2, 2000},
    3:  {3, 1000},
    4:  {4, 2000},
    5:  {5, 1000},
    6:  {6, 2000},
    7:  {7, 1000},
    8:  {8, 2000},
    9:  {9, 1000},
    10: {10, 2000},
}

func worker(w int, jobs <-chan Vertex, results chan<- int) {
    for j := 1; j <= len(m); j++ {
        a, b := <-jobs.id, <-jobs.waitime
        fmt.Println("worker", w, "started job", a)
        //time.Sleep(time.Sleep(time.Duration(b)))
        fmt.Println("worker", w, "finished job", a)
        results <- j * 2
    }
}

func main() {
    //n := 5
    jobs := make(chan Vertex, 100)
    results := make(chan int, 100)

    for w := 1; w <= 5; w++ {
        go worker(w, jobs, results)
    }
    fmt.Println(len(m))
    for j := 1; j <= len(m); j++ {
        jobs <- m[j]
    }
    //close(jobs)

    for a := 1; a <= len(m); a++ {
        <-results
    }
}
英文:

With reference to this example, i want to add a pre-defined delay between job initialization and termination. I have stored the data i.e. jobid and waittime in map. Then i have copied the entire map in a channel of same struct type as map. But i am not able to fetch the map values in go routine calls. Please help me, I am new to Go.

package main
import &quot;fmt&quot;
type Vertex struct {
id, waitime int
}
var m = map[int]Vertex{
1:  {1, 1000},
2:  {2, 2000},
3:  {3, 1000},
4:  {4, 2000},
5:  {5, 1000},
6:  {6, 2000},
7:  {7, 1000},
8:  {8, 2000},
9:  {9, 1000},
10: {10, 2000},
}
func worker(w int, jobs &lt;-chan Vertex, results chan&lt;- int) {
for j := 1; j &lt;= len(m); j++ {
a, b := &lt;-jobs.id, &lt;-jobs.waitime
fmt.Println(&quot;worker&quot;, w, &quot;started job&quot;, a)
//time.Sleep(time.Sleep(time.Duration(b)))
fmt.Println(&quot;worker&quot;, w, &quot;finished job&quot;, a)
results &lt;- j * 2
}
}
func main() {
//n := 5
jobs := make(chan Vertex, 100)
results := make(chan int, 100)
for w := 1; w &lt;= 5; w++ {
go worker(w, jobs, results)
}
fmt.Println(len(m))
for j := 1; j &lt;= len(m); j++ {
jobs &lt;- m[j]
}
//close(jobs)
for a := 1; a &lt;= len(m); a++ {
&lt;-results
}
}

答案1

得分: 5

你的代码有几个问题。

首先,你不能直接从通道中访问结构体成员。也就是说,这一行是错误的:

a, b := <-jobs.id, <-jobs.waitime

jobs 是一个通道,它没有名为 idwaittime 的成员。这些是结构体 Vertex 的成员,它们应该被传输。将这一行改为:

job := <-jobs
a, b := job.id, job.waitime

但是现在,你的代码声明了 b,但没有使用它。要修复这个问题,取消对 time.Sleep 的注释:

time.Sleep(time.Sleep(time.Duration(b))) !INCORRECT CALL

但是这个调用是完全错误的。time.Sleep 需要一个 time.Duration 类型的参数,并且不返回任何值。要修复这个问题,进行以下更改:

time.Sleep(time.Duration(b) * time.Millisecond)

这样应该可以让你的代码运行起来。

英文:

Couple of things are wrong with your code.

First, you can't access struct members directly off the channel. That is, this line is wrong:

a, b := &lt;-jobs.id, &lt;-jobs.waitime

jobs is a channel. It does not have any members called id or waittime. These are the members of the struct Vertex it's supposed to transfer. Change this line to:

job := &lt;-jobs
a, b := job.id, job.waitime

But now, your code declares b but does not use it. To fix, uncomment the call to time.Sleep:

time.Sleep(time.Sleep(time.Duration(b))) !INCORRECT CALL

But this call is completely wrong. time.Sleep expects an argument of type time.Duration and returns nothing. To fix, make the following changes:

time.Sleep(time.Duration(b) * time.Millisecond)

This should get your code running.

答案2

得分: 1

将代码中的部分进行翻译如下:

将:

a, b := <-jobs.id, <-jobs.waitime

改为:

job := <-jobs
a, b := job.id, job.waitime

jobs 是一个通道而不是一个顶点。

英文:

Change:

a, b := &lt;-jobs.id, &lt;-jobs.waitime

to:

job := &lt;-jobs
a, b := job.id, job.waitime

jobs is a channel not a Vertex.

huangapple
  • 本文由 发表于 2017年6月7日 17:00:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/44408089.html
匿名

发表评论

匿名网友

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

确定