第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

json.解碼一個(gè) TCP 連接并在 Go 中同時(shí)記錄原始數(shù)據(jù)

json.解碼一個(gè) TCP 連接并在 Go 中同時(shí)記錄原始數(shù)據(jù)

Go
繁星點(diǎn)點(diǎn)滴滴 2022-06-21 09:55:36
我正在使用 ajson.Decoder將 a 解碼net.Conn為自定義結(jié)構(gòu)。我還想記錄在net.Conn不修改的情況下發(fā)送的原始 JSON 字符串。我正在尋找一種有效的方法來(lái)實(shí)現(xiàn)這一目標(biāo)。我目前有以下代碼:reader := bufio.NewReader(conn)   // conn is a net.Conndec := json.NewDecoder(reader)for {       // How can I also log the original data before decoding it into the struct?    var dst customStruct    if err = dec.Decode(&dst); err == io.EOF {        break    } else if err != nil {        log.Errorf("failed to decode message: %s", err)        break    }    // do something with dst...}
查看完整描述

2 回答

?
MMTTMM

TA貢獻(xiàn)1869條經(jīng)驗(yàn) 獲得超4個(gè)贊

使用iotest.NewReadLogger在讀取數(shù)據(jù)時(shí)記錄數(shù)據(jù):


prefix := fmt.Sprintf("%p:", conn)

reader := bufio.NewReader(iotest.NewReadLogger(prefix, conn)) 

dec := json.NewDecoder(reader)


for {   


    var dst customStruct

    if err = dec.Decode(&dst); err == io.EOF {

        break

    } else if err != nil {

        log.Errorf("failed to decode message: %s", err)

        break

    }


    // do something with dst...


}

    

如果讀取的記錄器輸出不符合您的口味,則復(fù)制實(shí)現(xiàn)并根據(jù)需要進(jìn)行調(diào)整。實(shí)現(xiàn)非常簡(jiǎn)單。


查看完整回答
反對(duì) 回復(fù) 2022-06-21
?
大話西游666

TA貢獻(xiàn)1817條經(jīng)驗(yàn) 獲得超14個(gè)贊

您可以使用ioutil.ReadAll()然后從字節(jié)中打印和解碼。但是由于您在不使用緩沖區(qū)的情況下讀取所有字節(jié),因此理想情況下應(yīng)該將其放在debug標(biāo)志檢查之后。


bs,err := ioutil.ReadAll()

if err != nil {

    panic(err)

}


log.Printf("output: %s", string(bs))


var dst customStruct

if err := json.Unmarshal(bs, &dst); err != nil {

    panic(err)

}


查看完整回答
反對(duì) 回復(fù) 2022-06-21
  • 2 回答
  • 0 關(guān)注
  • 141 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)