亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

2-3 節代碼,我的什么沒輸出呢,老鐵們幫忙看下??

package?main

import?(
???"strings"
???"fmt"
???"time"
???"os"
???"bufio"
???"io"
)

type?Reader3?interface?{
???read(rc?chan?[]byte)
}

type?Writer3?interface?{
???write(wc?chan?string)
}
type?ReadFromFile3?struct?{
???path?string
}
type?WriteToInfluxDB3?struct?{
???influxDBDsn?string
}
type?LogProcess3?struct?{
???rc?????chan?[]byte
???wc?????chan?string
???reader?Reader3
???writer?Writer3
}

//?1?讀取模塊
func?(r?*ReadFromFile3)?read(rc?chan?[]byte)?{
???fmt.Println("Begin?read?File")
???file,?e?:=?os.OpenFile(r.path,?os.O_WRONLY|os.O_APPEND,?os.ModePerm)
???if?e?!=?nil?{
??????panic(fmt.Sprintf("open?file?error:%s",?e.Error()))
???}

???//?從文件末尾開始逐行讀取文件內容
???file.Seek(0,?2)
???rd?:=?bufio.NewReader(file)

???for?{
??????line,?err?:=?rd.ReadBytes('\n')

??????if?err?!=?io.EOF?{?//?到結尾
?????????fmt.Println(line)
?????????time.Sleep(500?*?time.Millisecond)
?????????continue
??????}?else?if?err?!=?nil?{
?????????panic(fmt.Sprintf("ReadBytes?error:%s",?err.Error()))
??????}
??????fmt.Println(">?",line)
??????//?rc?<-?line
??????rc?<-?line[:len(line)-1]
???}

}

func?(l?*LogProcess3)?process()?{
???//?解析模塊
???
???for?v?:=?range?l.rc?{
??????l.wc?<-?strings.ToUpper(string(v))
???}
}

//?3?寫入模塊
func?(w?*WriteToInfluxDB3)?write(wc?chan?string)?{
???fmt.Println(">>?",*&wc)?//?>>??0xc4200760c0?這里為什么是地址呢?
???for?v?:=?range?wc?{
??????fmt.Printf(v)
???}
}

func?main()?{
???read?:=?&ReadFromFile3{
??????path:?"/Users/xyang/go_code/src/xyang.com/logcollect/data/access.log",
??????//path:?"data/access.log",
???}

???writer?:=?&WriteToInfluxDB3{
??????influxDBDsn:?"username=?&password=?",
???}

???lp?:=?&LogProcess3{
??????rc:?????make(chan?[]byte),
??????wc:?????make(chan?string),
??????reader:?read,
??????writer:?writer,
???}

???go?lp.reader.read(lp.rc)
???go?lp.process()
???go?lp.writer.write(lp.wc)

???time.Sleep(30?*?time.Second)
}


正在回答

1 回答

file,?e?:=?os.OpenFile(r.path,?os.O_WRONLY|os.O_APPEND,?os.ModePerm)

改為:?

file,?e?:=?os.Open(r.path)



if?err?!=?io.EOF?改為:?if?err?==?io.EOF
0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消
Go并發編程案例解析
  • 參與學習       15263    人
  • 解答問題       56    個

課程帶你通過一個真實的線上日志監控系統學習Golang以及并發的編程思想。

進入課程

2-3 節代碼,我的什么沒輸出呢,老鐵們幫忙看下??

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號