英文:
How should I write goroutine?
问题
现在我有以下代码:
package main
import (
"crypto/md5"
"encoding/hex"
"fmt"
"io"
"math"
"strconv"
)
func Md5Str(str string) string {
m := md5.New()
io.WriteString(m, str)
return hex.EncodeToString(m.Sum(nil))
}
func compute(size int) int {
num := int(math.Floor(float64(size+256*1024-1)) / 256 / 1024)
return num
}
func out(s string) string {
return "Hello: " + s
}
func main() {
num := compute(4194304)
for i := 0; i < num; i++ {
key := Md5Str("503969280ff8679135937ad7d23b06c5" + "_" + strconv.Itoa(i))
res := out(key + "_" + strconv.Itoa(i))
fmt.Println(res)
}
}
它可以正常运行。
我想要并发运行这段代码,因为如果out
函数运行时间很长,使用并发运行可以节省时间。
英文:
Now I have the following code:
package main
import (
"crypto/md5"
"encoding/hex"
"fmt"
"io"
"math"
"strconv"
)
func Md5Str(str string) string {
m := md5.New()
io.WriteString(m, str)
return hex.EncodeToString(m.Sum(nil))
}
func compute(size int) int {
num := int(math.Floor(float64(size+256*1024-1)) / 256 / 1024)
return num
}
func out(s string) string {
return "Hello: " + s
}
func main() {
num := compute(4194304)
for i := 0; i < num; i++ {
key := Md5Str("503969280ff8679135937ad7d23b06c5" + "_" + strconv.Itoa(i))
res := out(key + "_" + strconv.Itoa(i))
fmt.Println(res)
}
}
And it can run right.
I want to concurrency run the code, because if the out
function run long time and the concurrency run will save time.
答案1
得分: 1
感谢 @zerkms,我根据Go by Example: Worker Pools重新编写了以下代码:
package main
import (
"crypto/md5"
"encoding/hex"
"fmt"
"io"
"math"
"strconv"
)
func Md5Str(str string) string {
m := md5.New()
io.WriteString(m, str)
return hex.EncodeToString(m.Sum(nil))
}
func compute(size int) int {
num := int(math.Floor(float64(size+256*1024-1)) / 256 / 1024)
return num
}
func out(s string, jobs <-chan int, results chan<- string) {
for j := range jobs {
fmt.Println("Hello: " + s + strconv.Itoa(j))
results <- s
}
}
func main() {
num := compute(4194304)
jobs := make(chan int, num)
results := make(chan string, num)
for i := 0; i < num; i++ {
key := Md5Str("503969280ff8679135937ad7d23b06c5" + "_" + strconv.Itoa(i))
go out(key+"_"+strconv.Itoa(i), jobs, results)
}
for j := 0; j < num; j++ {
jobs <- j
}
close(jobs)
for k := 0; k < num; k++ {
<-results
}
}
它可以正常运行。
<details>
<summary>英文:</summary>
Thanks @zerkms, I re-wrote the following code base on [Go by Example: Worker Pools](https://gobyexample.com/worker-pools)
package main
import (
"crypto/md5"
"encoding/hex"
"fmt"
"io"
"math"
"strconv"
)
func Md5Str(str string) string {
m := md5.New()
io.WriteString(m, str)
return hex.EncodeToString(m.Sum(nil))
}
func compute(size int) int {
num := int(math.Floor(float64(size+256*1024-1)) / 256 / 1024)
return num
}
func out(s string, jobs <-chan int, results chan<- string) {
for j := range jobs {
fmt.Println("Hello: " + s + strconv.Itoa(j))
results <- s
}
}
func main() {
num := compute(4194304)
jobs := make(chan int, num)
results := make(chan string, num)
for i := 0; i < num; i++ {
key := Md5Str("503969280ff8679135937ad7d23b06c5" + "_" + strconv.Itoa(i))
go out(key+"_"+strconv.Itoa(i), jobs, results)
}
for j := 0; j < num; j++ {
jobs <- j
}
close(jobs)
for k := 0; k < num; k++ {
<-results
}
}
It can run right.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论