dongzi1209 2019-03-06 15:25
浏览 86

引用未定义的标识符字节。

I am trying to build a simple webserver. I want to replace all with <br>. I wanted to use bytes for this, because my page body is stored as an []byte.

I use bytes.ReplaceAll() for this. But it keeps saying that it's a reference to undefined identifier.

Can someone tell me why? I tried the exact same line within an online Compiler and it worked just fine. Do I miss the library?

See my code below:

import (
    "bytes"
    "html/template"
    "io/ioutil"
    "log"
    "net/http"
    "regexp"
)

type Page struct {
    Title string
    Body  []byte
}

func editHandler(w http.ResponseWriter, r *http.Request, title string) {
    p, err := loadPage(title)
    if err != nil {
        p = &Page{Title: title}
    }
    // THE LINE THAT CAUSES TROUBLE
    p.Body = bytes.ReplaceAll(p.Body, []byte("
"), []byte("<br>"))

    renderTemplate(w, "edit", p)
}
  • 写回答

2条回答 默认 最新

  • doujiaozhan4397 2019-03-06 15:30
    关注

    The bytes package is part of the standard library, so it is unlikely that you don't have it if you have the go tool available to you.

    But do note that bytes.ReplaceAll() was added in Go 1.12, so if you have an older Go SDK, this function will not be available to you.

    Execute go version to find out. Get the latest Go from the official site: https://golang.org/dl/

    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集