What is the difference between = and <- in golang

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

What is the difference between = and <- in golang

问题

func worker(id int, jobs <-chan int, results chan<- int) {
for j := range jobs {
fmt.Println("worker", id, "processing job", j)
time.Sleep(time.Second)
results <- j * 2
}
}

func main() {
t := time.Now()
fmt.Println(t)
jobs := make(chan int, 100)
results := make(chan int, 100)
for w := 1; w <= 4; w++ {
go worker(w, jobs, results)
}
for j := 1; j <= 20; j++ {
jobs <- j
}
close(jobs)
for a := 1; a <= 20; a++ {
<-results
}

t = time.Now()
fmt.Println(t)

}

我对"<-"感到困惑,我找不到任何相关的文档关于"<-"。那么"<-"和"="有什么区别?为什么我不能在这里使用"="?

英文:
func worker(id int, jobs &lt;-chan int, results chan&lt;- int) {
    for j := range jobs {
	    fmt.Println(&quot;worker&quot;, id, &quot;processing job&quot;, j)
		time.Sleep(time.Second)
    	results &lt;- j * 2
	}
}

func main() {
    t := time.Now()
	fmt.Println(t)
    jobs := make(chan int, 100)
	results := make(chan int, 100)
	for w := 1; w &lt;= 4; w++ {
    	go worker(w, jobs, results)
    }
	for j := 1; j &lt;= 20; j++ {
    	jobs &lt;- j
	}
    close(jobs)
	for a := 1; a &lt;= 20; a++ {
    	&lt;-results
	}

    t = time.Now()
	fmt.Println(t)
}

I am confused of the "<-" and I can not find any related documents about "<-". So what is the difference between <- and =?? why I can not use = here?

答案1

得分: 21

= 运算符 处理变量赋值,就像大多数语言一样。它表示想要更新标识符引用的值的想法。&lt;- 运算符 表示将值从通道传递给引用的想法。如果将通道视为使用赋值运算符 = 分配队列的引用,则接收运算符 &lt;- 等效于从队列中出队并将项目的值分配给目标变量。

由于类型不匹配,您不能互换使用这些运算符。请注意链接到 Go 规范,其中更详细地介绍了这些运算符。

英文:

The = operator deals with variable assignment as in most languages. It expresses the idea of wanting to update the value that an identifier references. The &lt;- operator represents the idea of passing a value from a channel to a reference. If you think of the channel as a queue using an assignment operator = would assign the reference to the queue to the target variable. The receive operator &lt;- is equivalent to dequeuing from the queue and assigning the value of the item to the target variable.

You cannot use the operators interchangeably because of a type mismatch. Please note the links to the Go specification which speak at greater length to the operators.

答案2

得分: 16

这与Go语言中的通道有关。你可能会认为它与其他语言中的赋值相关。在你的代码中,一个值“j”被发送到通道“jobs”。

https://gobyexample.com/channels

英文:

This is related to channels in Go. You are thinking it's related to assignment as in other languages. In your code, a value "j" is being sent to the channel "jobs".

https://gobyexample.com/channels

答案3

得分: 11

"=" 是赋值操作符,就像其他编程语言一样。

  • <- 是一个只能用于通道的操作符,它表示从通道中发送接收消息。

  • 通道 是Go语言中的一个重要概念,特别是在并发编程中。你可以尝试查看这个通道教程页面来了解它的使用场景。

英文:
  • "=" is assignment,just like other language.

  • <- is a operator only work with channel,it means put or get a message from a channel.

  • channel is an important concept in go,especially in concurrent programming.you can try this Channel TourPage to see its using scene.

答案4

得分: 0

这个例子旨在说明通道和<-符号的用法,如果还是有困惑,提供注释和解释应该会有所帮助:

func worker(id int, jobs <-chan int, results chan<- int) {
    // 每个worker都是一个goroutine
    // 它有一个整数id,
    // `jobs <-chan int`的表示意思是`jobs`是一个整数类型的通道,
    //   并且`worker`只能从通道中读取
    // `results chan<- int`的表示意思是`results`也是一个整数类型的通道,
    //   并且`worker`只能向通道中写入

    for j := range jobs {
        fmt.Println("worker", id, "processing job", j)
        time.Sleep(time.Second)
        results <- j * 2 // 这个表示意思是将`j * 2`写入到`results`通道中
    }
    // 所以worker所做的工作是乘以2
}

func main() {
    t := time.Now()
    fmt.Println(t)

    // jobs和results通道的容量为100
    jobs := make(chan int, 100)
    results := make(chan int, 100)

    // 我们启动4个worker goroutine
    for w := 1; w <= 4; w++ {
        go worker(w, jobs, results)
    }

    // 我们用20个作业填充jobs队列
    for j := 1; j <= 20; j++ {
        jobs <- j // 这个语法表示将`j`写入到`jobs`通道中
    }
    close(jobs) // 表示我们不会再添加更多的作业

    // 我们等待直到从results队列中取出了20个期望的结果
    for a := 1; a <= 20; a++ {
        <-results // 这个语法表示我们从results队列中取出一个元素并丢弃它(因为我们没有将其赋值给任何变量)
    }

    // 统计所有这些工作所花费的时间
    t = time.Now()
    fmt.Println(t)
}

以上是代码的翻译部分。

英文:

This example is meant to illustrate the usage of channels and of the &lt;- notation, so if it's still confusing, providing annotation/explanation should help:

func worker(id int, jobs &lt;-chan int, results chan&lt;- int) {
    // each worker will be a goroutine
    // it has an integer id, 
    // the notation `jobs &lt;-chan int` means `jobs` is a channel
    //   of ints, and that `worker` can only read from the channel
    // the notation `results chan&lt;- int` means results is also
    // a channel of ints, and that `worker` can only write to 
    //   the channel

    for j := range jobs {
        fmt.Println(&quot;worker&quot;, id, &quot;processing job&quot;, j)
        time.Sleep(time.Second)
        results &lt;- j * 2 // This notation means `j * 2` is 
                         // written to the channel `results`
    }
    // so the job the worker is doing is multiplication by 2
}

func main() {
    t := time.Now()
    fmt.Println(t)

    // jobs and results channels of capacity 100
    jobs := make(chan int, 100)
    results := make(chan int, 100)

    // We start running 4 worker goroutines
    for w := 1; w &lt;= 4; w++ {
        go worker(w, jobs, results)
    }
    
    // We load up the jobs queue with 20 jobs
    for j := 1; j &lt;= 20; j++ {
        jobs &lt;- j // This syntax means `j` is written 
                  // to the channel `jobs`
    }
    close(jobs) // Signals that we won&#39;t be adding any more jobs

    // We wait until we&#39;ve pulled 20 expected 
    // results from the results queue
    for a := 1; a &lt;= 20; a++ {
        &lt;-results // This syntax means we pull an element out of 
                  // the results queue and discard it (since we 
                  // aren&#39;t assigning to any variable)
    }

    // Count how much time all that work took
    t = time.Now()
    fmt.Println(t)
}

huangapple
  • 本文由 发表于 2015年10月25日 09:59:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/33325395.html
匿名

发表评论

匿名网友

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

确定