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

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Go:在循環中附加字節切片

Go:在循環中附加字節切片

Go
陪伴而非守候 2021-11-22 10:47:02
我是 Go 的新手,所以如果已經回答了這個問題,我深表歉意,我正在嘗試在 Go 中附加一個字節片,但我沒有找到解決方案的運氣。我需要拆分文件的第一行,我已經完成了;并將其余部分寫入字節切片以供事后解析。到目前為止,代碼如下所示:// Here we extract the first line to name our title and categoryvar title, category stringvar content []bytein, err := os.Open(file)utils.CheckErr(err, "could not open file: "+file)defer in.Close()// open filescanner := bufio.NewScanner(in)lineCount := 1for scanner.Scan() {    if lineCount == 1 {        // assign title and category        splitString := strings.Split(scanner.Text(), "::")        title = splitString[0]        category = splitString[1]        fmt.Println("title: " + title + "category" + category) // usage to prevent compiler whine    } else {        // push the rest into an array to be parsed as jade        line := scanner.Bytes()        content = append(content, line) // The question is what goes here?    }    lineCount++}我試過使用 append 但這只給了我不能使用 line (type []byte) 作為 type byte in append 的錯誤
查看完整描述

2 回答

?
哈士奇WWW

TA貢獻1799條經驗 獲得超6個贊

我相信你只是在尋找; content = append(content, line...)


查看完整回答
反對 回復 2021-11-22
?
FFIVE

TA貢獻1797條經驗 獲得超6個贊

https://golang.org/ref/spec#Appending_and_copying_slices

可能有重復但直到我找到它...

通過在末尾添加“...”來解決您的問題,line因此它看起來像:

content = append(content, line...)


查看完整回答
反對 回復 2021-11-22
  • 2 回答
  • 0 關注
  • 202 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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