How to Convert Markdown to HTML using Go

Cloudmersive
2 min readNov 25, 2022

--

While vanilla HTML is a readable & relatively easy-to-write programming language, it’s still inconvenient to spend hours writing new HTML syntax when a simpler alternative exists: Markdown. With the proper conversion tool available to you, transitioning Markdown syntax to HTML makes formatting web pages a breeze. To that end, our Markdown to HTML conversion API represents a secure service you can build into your applications, ensuring you can convert dozens of markdown files to full-blown HTML syntax in seconds.

With a free-tier Cloudmersive API key, you can use this API at zero cost; all you need to do is register a free account on our website & follow instructions below to structure your API call using ready-to-run Golang code examples.

Calling this API is extremely straightforward. Once you have your API key handy, copy the below code examples into your file, and include your API key string within the relevant field below. After that, you’ll just need to parse your file path in the designated header, and you’re ready to call the function:

package main

import (
"fmt"
"bytes"
"mime/multipart"
"os"
"path/filepath"
"io"
"net/http"
"io/ioutil"
)

func main() {

url := "https://api.cloudmersive.com/convert/web/md/to/html"
method := "POST"

payload := &bytes.Buffer{}
writer := multipart.NewWriter(payload)
file, errFile1 := os.Open("/path/to/file")
defer file.Close()
part1,
errFile1 := writer.CreateFormFile("inputFile",filepath.Base("/path/to/file"))
_, errFile1 = io.Copy(part1, file)
if errFile1 != nil {
fmt.Println(errFile1)
return
}
err := writer.Close()
if err != nil {
fmt.Println(err)
return
}


client := &http.Client {
}
req, err := http.NewRequest(method, url, payload)

if err != nil {
fmt.Println(err)
return
}
req.Header.Add("Content-Type", "multipart/form-data")
req.Header.Add("Apikey", "YOUR-API-KEY-HERE")

req.Header.Set("Content-Type", writer.FormDataContentType())
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()

body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}

--

--

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.