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

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

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))

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:

确定