为了账号安全,请及时绑定邮箱和手机立即绑定

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 关注
  • 176 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信