Go: Using using a string in a []byte function type argument?

huangapple go评论119阅读模式
英文:

Go: Using using a string in a []byte function type argument?

问题

我是Go的初学者。我正在尝试使用blackfriday。这是我的代码:

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/russross/blackfriday"
  5. )
  6. func main() {
  7. input := "this is a test"
  8. output := blackfriday.MarkdownCommon([]byte(input))
  9. fmt.Println(string(output))
  10. }

然而,我遇到了一个错误:

  1. alex@alex-K43U:~/go/src/m2kgo$ go run m2kgo.go
  2. # command-line-arguments
  3. ./m2kgo.go:20: cannot use input (type string) as type []byte in argument to blackfriday.MarkdownCommon

所以我尝试将参数转换为[]byte

  1. output := blackfriday.MarkdownCommon([]byte(input))

这样输出的是字节:

  1. alex@alex-K43U:~/go/src/m2kgo$ go run m2kgo.go
  2. [60 112 62 116 104 105 115 32 105 115 32 97 32 116 101 115 116 60 47 112 62 10]

如何打印生成的HTML而不是字节?

英文:

I'm begginer to Go. I'm trying to use blackfriday (a Go Markdown parser). This is the code:

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/russross/blackfriday"
  5. )
  6. func main() {
  7. input := "this is a test"
  8. output := blackfriday.MarkdownCommon(input)
  9. fmt.Println(output)
  10. }

I got an error, though:

  1. alex@alex-K43U:~/go/src/m2kgo$ go run m2kgo.go
  2. # command-line-arguments
  3. ./m2kgo.go:20: cannot use input (type string) as type []byte in argument to blackfriday.MarkdownCommon

So I tried turning the argument into []byte:

  1. output := blackfriday.MarkdownCommon([]byte(input))

This output the bytes though:

  1. alex@alex-K43U:~/go/src/m2kgo$ go run m2kgo.go
  2. [60 112 62 116 104 105 115 32 105 115 32 97 32 116 101 115 116 60 47 112 62 10]

How can I print the generated HTML instead of the bytes?

答案1

得分: 4

将其转换回字符串。

fmt.Println(string(output))

英文:

Convert it back to a string.

fmt.Println(string(output))

huangapple
  • 本文由 发表于 2015年2月27日 12:06:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/28757340.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定