Golang的并发编程(1)

package main

import (
	"fmt"
	"strings"
	"time"
)

type LogProcess struct {
	rc chan string
	wc chan string
	path string
	influxDBDsn string
}

func (l *LogProcess) ReadFromFile()  {
	//读取
	line := "message"
	l.rc <- line

}

func (l *LogProcess) Process() {
	//解析
	data := <-l.rc
	l.wc <- strings.ToUpper(data)
}

func (l *LogProcess) WriteToInfluxDB() {
	//写入
	fmt.Println(<-l.wc)
}


func main()  {
	lp := &LogProcess{
		rc: make(chan string),
		wc: make(chan string),
		path: "/tmp/access.log",
		influxDBDsn: "username&password..",
	}

	go lp.ReadFromFile()
	go lp.Process()
	go lp.WriteToInfluxDB()

	time.Sleep(1*time.Second)
}
上一篇:Go 面试每天一篇(第 9 天)


下一篇:channel