英文:
Go: Using using a string in a []byte function type argument?
问题
我是Go的初学者。我正在尝试使用blackfriday。这是我的代码:
package main
import (
"fmt"
"github.com/russross/blackfriday"
)
func main() {
input := "this is a test"
output := blackfriday.MarkdownCommon([]byte(input))
fmt.Println(string(output))
}
然而,我遇到了一个错误:
alex@alex-K43U:~/go/src/m2kgo$ go run m2kgo.go
# command-line-arguments
./m2kgo.go:20: cannot use input (type string) as type []byte in argument to blackfriday.MarkdownCommon
所以我尝试将参数转换为[]byte
:
output := blackfriday.MarkdownCommon([]byte(input))
这样输出的是字节:
alex@alex-K43U:~/go/src/m2kgo$ go run m2kgo.go
[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:
package main
import (
"fmt"
"github.com/russross/blackfriday"
)
func main() {
input := "this is a test"
output := blackfriday.MarkdownCommon(input)
fmt.Println(output)
}
I got an error, though:
alex@alex-K43U:~/go/src/m2kgo$ go run m2kgo.go
# command-line-arguments
./m2kgo.go:20: cannot use input (type string) as type []byte in argument to blackfriday.MarkdownCommon
So I tried turning the argument into []byte
:
output := blackfriday.MarkdownCommon([]byte(input))
This output the bytes though:
alex@alex-K43U:~/go/src/m2kgo$ go run m2kgo.go
[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))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论