douxing7101 2015-11-04 21:13
浏览 21

执行:在循环中附加字节片[重复]

This question already has an answer here:

I'm new to Go, so I apologise if this has already been answered, I'm trying to append a byte slice in Go and I am not having any luck finding a solution. I need to split off the first line of the file, which I've done; And write the rest into a byte slice to be parsed after the fact. So far the code looks like this:

// 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++
}

I've tried using append but that only gives me the error that cannot use line (type []byte) as type byte in append

</div>
  • 写回答

2条回答 默认 最新

  • douyou1857 2015-11-04 21:18
    关注

    I believe you're simply looking for; content = append(content, line...)

    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题