gpt4 book ai didi

arrays - Go:在循环中追加 byte slice

转载 作者:IT王子 更新时间:2023-10-29 02:29:56 26 4
gpt4 key购买 nike

<分区>

我是 Go 的新手,所以如果这个问题已经得到解答,我深表歉意,我正在尝试在 Go 中附加一个字节 slice ,但我没有找到解决方案。我需要拆分文件的第一行,我已经完成了;并将其余部分写入 byte slice 以供事后解析。到目前为止,代码如下所示:

// Here we extract the first line to name our title and category
var title, category string
var content []byte
in, err := os.Open(file)
utils.CheckErr(err, "could not open file: "+file)
defer in.Close()
// open file
scanner := bufio.NewScanner(in)
lineCount := 1
for 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) 作为 append 中的 type byte

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com