Go 练习

教你上传本地代码到github

程序1

package main

import (
    "fmt"
    "sort/pipeline"
)

func main() {
    p := pipeline.ArraySource(3, 2, 6, 7, 4)
    for {
        if num, ok := <-p; ok {
            fmt.Println(num)
        } else {
            break
        }
    }
}
package pipeline
func ArraySource(a ...int) <-chan int {
    out := make(chan int)
    go func() {
        for _, v := range a {
            out <- v
        }
        close(out)
    }()
    return out
}

程序2

main.go

package main
import (
    "fmt"
    "os"
    "sort/pipeline"
)

func main() {
    file, err := os.Create("small.in")
    if err != nil {
        panic(err)
    }
    p := pipeline.RandomSource(10)
    pipeline.WriterSink(file, p)

    file, err = os.Open("small.in")
    if err != nil {
        panic(err)
    }
    p = pipeline.ReaderSource(file)
    for v := range p {
        fmt.Println(v)
    }
    mergeDemo()

}

func mergeDemo() {
    p := pipeline.Merge(
        pipeline.InMemSort(pipeline.ArraySource(3, 2, 6, 7, 4)),
        pipeline.InMemSort(pipeline.ArraySource(7, 4, 0, 3, 2, 13, 8)))
    for v := range p {
        fmt.Println(v)
    }
}

pipeline.go

package pipeline

import (
    "encoding/binary"
    "io"
    "math/rand"
    "sort"
)

func ArraySource(a ...int) <-chan int {
    out := make(chan int)
    go func() {
        for _, v := range a {
            out <- v
        }
        close(out)
    }()
    return out
}

func InMemSort(in <-chan int) <-chan int {
    out := make(chan int)
    go func() {
        //Read into memory
        a := []int{}
        for v := range in {
            a = append(a, v)
        }
        //Sort
        sort.Ints(a)
        //Output
        for _, v := range a {
            out <- v
        }
        close(out)
    }()
    return out
}

func Merge(in1, in2 <-chan int) <-chan int {
    out := make(chan int)
    go func() {
        v1, ok1 := <-in1
        v2, ok2 := <-in2
        for ok1 || ok2 {
            if !ok2 || (ok1 && v1 <= v2) {
                out <- v1
                v1, ok1 = <-in1
            } else {
                out <- v2
                v2, ok2 = <-in2
            }
        }
        close(out)
    }()
    return out
}

func ReaderSource(reader io.Reader) <-chan int {
    out := make(chan int)
    go func() {
        buffer := make([]byte, 8)
        for {
            n, err := reader.Read(buffer)
            if n > 0 {
                v := int(
                    binary.BigEndian.Uint64(buffer))
                out <- v
            }
            if err != nil {
                break
            }
        }
        close(out)
    }()
    return out
}

func WriterSink(writer io.Writer, in <-chan int) {
    for v := range in {
        buffer := make([]byte, 8)
        binary.BigEndian.PutUint64(buffer, uint64(v))
        writer.Write(buffer)
    }
}

func RandomSource(count int) <-chan int {
    out := make(chan int)
    go func() {
        for i := 0; i < count; i++ {
            out <- rand.Int()
        }
        close(out)
    }()
    return out
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值