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

2019-08-11 15:08发布

问题:

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:

Convert it back to a string.

fmt.Println(string(output))



标签: go markdown