To convert a string to a HTML-encoded string using strconv in Golang, you can use the EscapeHTML
function provided by the strconv
package. Here's an example:
package main
import (
"fmt"
"strconv"
)
func main() {
str := "Hello <World>!"
encodedStr := strconv.QuoteToHTML(str)
fmt.Println(encodedStr)
}
The QuoteToHTML
function converts special characters in the string to their corresponding HTML entities. In this example, it'll replace <
with <
and >
with >
, resulting in the output Hello <World>!
.
You can learn more about the strconv
package and its functions in the official Golang documentation: https://golang.org/pkg/strconv/