在 go 中调用如 blackfriday.markdowncommon 这类接受 []byte 参数的函数时,需先将 string 显式转换为 []byte;而函数返回的 []byte 也需显式转回 string 才能正常打印 html 内容。
Go 的类型系统严格区分 string 和 []byte:前者是只读的 UTF-8 编码字节序列,后者是可变的字节切片。虽然二者底层数据兼容,但不能隐式转换——这正是你遇到编译错误 cannot use input (type string) as type []byte 的根本原因。
✅ 正确做法分两步:
修正后的完整代码如下:
package main
import (
"fmt"
"github.com/russross/blackfriday"
)
func main() {
input := "this is a test"
output := blackfriday.MarkdownCommon([]byte(input)) // ✅ 转换为 []byte 传入
fmt.Println(string(output)) // ✅ 转回 string 后打印 HTML
}运行后将输出:
this is a test
⚠️ 注意事项:

总结:Go 中 string ↔ []byte 的双向转换是基础且高频操作,务必掌握其显式语法与适用场景——它不是“绕过类型系统”,而是尊重类型安全前提下的精准表达。