Go ahead and add the following highlighted lines to create a new all command to the Commands slice. This command will print all added tasks to the standard output:

. . .
func main() {
	app := &cli.App{
		Name:  "tasker",
		Usage: "A simple CLI program to manage your tasks",
		Commands: []*cli.Command{
			{
				Name:    "add",
				Aliases: []string{"a"},
				Usage:   "add a task to the list",
				Action: func(c *cli.Context) error {
					str := c.Args().First()
					if str == "" {
						return errors.New("Cannot add an empty task")
					}

					task := &Task{
						ID:        primitive.NewObjectID(),
						CreatedAt: time.Now(),
						UpdatedAt: time.Now(),
						Text:      str,
						Completed: false,
					}

					return createTask(task)
				},
			},
			{
				Name:    "all",
				Aliases: []string{"l"},
				Usage:   "list all tasks",
				Action: func(c *cli.Context) error {
					tasks, err := getAll()
					if err != nil {
						if err == mongo.ErrNoDocuments {
							fmt.Print("Nothing to see here.\nRun `add 'task'` to add a task")
							return nil
						}

						return err
					}

					printTasks(tasks)
					return nil
				},
			},
		},
	}

	err := app.Run(os.Args)
	if err != nil {
		log.Fatal(err)
	}
}

. . .
Then create the following functions immediately after createTask:

. . .
func getAll() ([]*Task, error) {
  // passing bson.D{{}} matches all documents in the collection
	filter := bson.D{{}}
	return filterTasks(filter)
}

func filterTasks(filter interface{}) ([]*Task, error) {
	// A slice of tasks for storing the decoded documents
	var tasks []*Task

	cur, err := collection.Find(ctx, filter)
	if err != nil {
		return tasks, err
	}

	for cur.Next(ctx) {
		var t Task
		err := cur.Decode(&t)
		if err != nil {
			return tasks, err
		}

		tasks = append(tasks, &t)
	}

	if err := cur.Err(); err != nil {
		return tasks, err
	}

  // once exhausted, close the cursor
	cur.Close(ctx)

	if len(tasks) == 0 {
		return tasks, mongo.ErrNoDocuments
	}

	return tasks, nil
}
Make sure to import the bson package:

package main

import (
	"context"
	"errors"
	"log"
	"os"
	"time"

	"github.com/urfave/cli/v2"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)
. . .
And import it into your main.go file along with the fmt package:

package main

import (
	"context"
	"errors"
  "fmt"
	"log"
	"os"
	"time"

	"github.com/urfave/cli/v2"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
	"gopkg.in/gookit/color.v1"
)
. . .
It will list all the tasks that you’ve added so far:

Output1: Learn Go
2: Read a book
Build and run your program with the all command:

go run main.go all
Before you can use the this package, install it with:

go get gopkg.in/gookit/color.v1

# Output:
# Outputgo: downloading gopkg.in/gookit/color.v1 v1.1.6
# go: gopkg.in/gookit/color.v1 upgrade => v1.1.6
Next, create a new printTasks function following your main function:

. . .
func printTasks(tasks []*Task) {
	for i, v := range tasks {
		if v.Completed {
			color.Green.Printf("%d: %s\n", i+1, v.Text)
		} else {
			color.Yellow.Printf("%d: %s\n", i+1, v.Text)
		}
	}
}
. . .

Recommend

How To Use Go with MongoDB Using the MongoDB Go Driver Step 4 — Listing all Tasks

How To Use Go with MongoDB Using the MongoDB Go Driver Step 3 — Creating a Task

How To Use Go with MongoDB Using the MongoDB Go Driver Step 2 — Creating a CLI Program

How To Use Go with MongoDB Using the MongoDB Go Driver Step 1 — Installing the MongoDB Go Driver

How To Use Go with MongoDB Using the MongoDB Go Driver Prerequisites

Installing Django on Ubuntu 12.04 Installing Django Installing Django from the Git Repository

Installing Django on Ubuntu 12.04 Installing Django Installing Django without Aptitude:

Installing Django on Ubuntu 12.04 Installing Django Installing Django with Aptitude:

Installing Django on Ubuntu 12.04 Optional installations

Installing Django on Ubuntu 12.04 Prerequisites

Cómo crear e instalar programas de Go Instalar programas Go con go install

Cómo crear e instalar programas de Go Cambiar el nombre del binario

Cómo crear e instalar programas de Go Compilar binarios de Go con go build

Cómo crear e instalar programas de Go Configurar y ejecutar el binario de Go

Cómo configurar claves de SSH en Ubuntu 20.04 Paso 4: Inhabilitar la autenticación con contraseña en su servidor

Cómo configurar claves de SSH en Ubuntu 20.04 Paso 3: Autenticación en el servidor de Ubuntu con claves de SSH

Cómo configurar claves de SSH en Ubuntu 20.04 Paso 2: Copiar la clave pública al servidor Ubuntu Copiar la clave pública de forma manual

Cómo configurar claves de SSH en Ubuntu 20.04 Paso 2: Copiar la clave pública al servidor Ubuntu Copiar clave pública usando SSH

Cómo configurar claves de SSH en Ubuntu 20.04 Paso 2: Copiar la clave pública al servidor Ubuntu Copiar clave pública usando ssh-copy-id

Cómo configurar claves de SSH en Ubuntu 20.04 Paso 1: Crear el par de claves

How To Install WordPress on Ubuntu 12.04 Step Four—Copy the Files

How To Install WordPress on Ubuntu 12.04 Step Three—Setup the WordPress Configuration

How To Install WordPress on Ubuntu 12.04 Step Two—Create the WordPress Database and User