Compiled, executable documentation

Go Markdown table examples

These listings come directly from checked-in Go source. Run them locally to exercise github.com/tzfqh/gmdtable; the website does not reimplement the package in the browser or send code to a remote execution service.

Run the basic command

The generator reads this listing from examples/basic/main.go. It supplies ordered headers and one row to the public Convert function.

go run ./examples/basic

Expected output

| Name     | Language |
| :------- | :------- |
| gmdtable | Go       |

Open the basic example source on GitHub

// Command basic demonstrates the public Markdown-table conversion API.
package main

import (
	"fmt"
	"log"

	"github.com/tzfqh/gmdtable"
)

func main() {
	headers := []string{"Name", "Language"}
	data := []map[string]interface{}{{"Name": "gmdtable", "Language": "Go"}}

	table, err := gmdtable.Convert(headers, data)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(table)
}

Use an example test

The source-derived ExampleConvert function is compiled, and its output comment is checked by go test ./.... This keeps the example synchronized with the public API and exact Markdown output.

go test ./...

Open the example test source on GitHub

package gmdtable_test

import (
	"fmt"

	"github.com/tzfqh/gmdtable"
)

func ExampleConvert() {
	headers := []string{"Name", "Language"}
	data := []map[string]interface{}{{"Name": "gmdtable", "Language": "Go"}}

	table, err := gmdtable.Convert(headers, data)
	if err != nil {
		panic(err)
	}
	fmt.Println(table)
	// Output:
	// | Name     | Language |
	// | :------- | :------- |
	// | gmdtable | Go       |
}

Extend the example safely

Add columns

Append header names in the required order and add matching keys to each row map.

Handle errors

Check the returned error when the headers or data may be empty, before using the generated table.