...

/

Creating the To-Do File

Creating the To-Do File

Learn how to define different methods in the to-do application.

Creating the todo.go file

Let’s create the todo.go file under the topmost todo directory in the program structure.

We define the package name as todo and include the import section:

package todo
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"time"
)
Importing the package

Next, we create the two data structures that will be used in this package. The first is the item struct and its fields: Task of type string, Done of type bool, CreatedAt of type time.Time, and CompletedAt also of type time.Time. Since we don’t want this type to be used outside this package, we don’t export it. We do this by defining its name, starting with a lowercase character:

// item struct represents a ToDo item
type item struct {
Task string
Done bool
CreatedAt time.Time
CompletedAt time.Time
}
Creating the item data structure


...

Go exported types

Note: In Go, the visibility of a type or function is controlled by the case of the first character of its name. Names ...