如何编写goroutine?

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

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 (
    &quot;crypto/md5&quot;
    &quot;encoding/hex&quot;
    &quot;fmt&quot;
    &quot;io&quot;
    &quot;math&quot;
    &quot;strconv&quot;
)

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 &quot;Hello: &quot; + s
}

func main() {
    num := compute(4194304)
    for i := 0; i &lt; num; i++ {
        key := Md5Str(&quot;503969280ff8679135937ad7d23b06c5&quot; + &quot;_&quot; + strconv.Itoa(i))
        res := out(key + &quot;_&quot; + 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 (
        &quot;crypto/md5&quot;
        &quot;encoding/hex&quot;
        &quot;fmt&quot;
        &quot;io&quot;
        &quot;math&quot;
        &quot;strconv&quot;
    )

    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 &lt;-chan int, results chan&lt;- string) {
        for j := range jobs {
            fmt.Println(&quot;Hello: &quot; + s + strconv.Itoa(j))
            results &lt;- s
        }
    }

    func main() {
        num := compute(4194304)
        jobs := make(chan int, num)
        results := make(chan string, num)

        for i := 0; i &lt; num; i++ {
            key := Md5Str(&quot;503969280ff8679135937ad7d23b06c5&quot; + &quot;_&quot; + strconv.Itoa(i))
            go out(key+&quot;_&quot;+strconv.Itoa(i), jobs, results)
        }

        for j := 0; j &lt; num; j++ {
            jobs &lt;- j
        }
        close(jobs)

        for k := 0; k &lt; num; k++ {
            &lt;-results
        }
    }

It can run right.

</details>



huangapple
  • 本文由 发表于 2017年3月28日 08:52:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/43058841.html
匿名

发表评论

匿名网友

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

确定