Introduction
Keeping a diary is a great way to reflect on your day and preserve memories. This application allows you to create and save daily diary entries using the Go programming language. You can write new entries each day and save them to a file on your computer for later reference.
Objective
The goal of this project is to help you understand how to create a simple text-based diary application in Go. It will allow users to input their diary entries and save them to a text file with the current date as part of the filename, ensuring that each entry is stored uniquely.
Code
package main
import (
"fmt"
"os"
"time"
"bufio"
)
func main() {
// Get the current date to name the diary file
currentDate := time.Now().Format("2006-01-02")
fileName := "diary_" + currentDate + ".txt"
// Open the file for appending or create it if it doesn't exist
file, err := os.OpenFile(fileName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
fmt.Println("Error opening file:", err)
return
}
defer file.Close()
// Ask the user for their diary entry
fmt.Println("Welcome to your diary application!")
fmt.Println("Please enter your diary entry for the day:")
// Read the user's input
scanner := bufio.NewScanner(os.Stdin)
var entry string
for scanner.Scan() {
entry = scanner.Text()
if entry == "" {
break
}
file.WriteString(entry + "\n")
fmt.Println("Diary entry saved.")
fmt.Println("Enter more text or press Enter to finish.")
}
if err := scanner.Err(); err != nil {
fmt.Println("Error reading input:", err)
}
fmt.Println("Your diary entry has been saved in", fileName)
}
Program Explanation
The Go program works as follows:
- Current Date: The current date is obtained using Go’s
time.Now().Format()
function, which ensures that each diary entry is saved in a unique file named with the current date (e.g.,diary_2025-01-31.txt
). - Opening the File: The program checks if the file for the specific date already exists. If it does, it opens the file in append mode, allowing new entries to be added. If the file doesn’t exist, it is created using the
os.OpenFile()
function. - Taking Input: The program prompts the user to write their diary entry. It uses the
bufio.NewScanner()
to read input from the user line by line. The user can input multiple lines of text. The program will stop reading when the user presses Enter without typing any text. - Saving the Entry: Each line of text entered by the user is saved to the file using the
file.WriteString()
function. This ensures that all the entries are appended to the file.
How to Run the Program
- Install Go: If you haven’t installed Go, you can download and install it from here.
- Create a New File: Copy the provided code into a new file, for example,
diary.go
. - Run the Program: Open your terminal and navigate to the directory where the
diary.go
file is located. Run the program using the command:go run diary.go
. - Input Your Entry: Follow the prompts to write and save your daily diary entry. The entry will be stored in a file named based on the current date.