The next step is to add a new command to your task manager program for creating new tasks. Let’s call it add:

. . .
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)
				},
			},
		},
	}

	err := app.Run(os.Args)
	if err != nil {
		log.Fatal(err)
	}
}
Then create a new struct to represent a single task in the database and insert it immediately preceding the main function:

. . .
type Task struct {
	ID        primitive.ObjectID `bson:"_id"`
	CreatedAt time.Time          `bson:"created_at"`
	UpdatedAt time.Time          `bson:"updated_at"`
	Text      string             `bson:"text"`
	Completed bool               `bson:"completed"`
}
. . .
Next, create a function that receives an instance of Task and saves it in the database. Add this snippet following the main function:

. . .
func createTask(task *Task) error {
	_, err := collection.InsertOne(ctx, task)
  return err
}
. . .
Next, import the go.mongodb.org/mongo-driver/bson/primitive, time, and errors packages:

package main

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

	"github.com/urfave/cli/v2"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)
. . .
Test it out by adding a few tasks using the add command. If successful, you’ll see no errors printed to your screen:

go run main.go add "Learn Go"
go run main.go add "Read a book"
Begin by opening up your main.go file:

nano main.go

Recommend

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

How To Install WordPress on Ubuntu 12.04 Step One—Download WordPress

How To Create React Elements with JSX Step 5 — Conditionally Showing Elements with Short Circuiting

How To Create React Elements with JSX Step 4 — Mapping Over Data to Create Elements