Go Thoughts is a whimsical CLI adventure built in Go, aimed at playfully hoarding and summoning your treasured one-liners, quirky quotes, wild ideas, and URLs from the depths of the command line abyss!
The project is a command-line interface (CLI) application built using the Go programming language. The purpose of the application is to store and manage basic thoughts, which consist of a unique identifier (ID) represented by a universally unique identifier (UUID) 🔑, content 💬, and date created 📅. The thoughts are stored locally on the user's computer as a comma-separated values (CSV) file in the home directory named thoughts.csv
🏠.
To build the project, I utilized the Go programming language and various third-party libraries, including uuid
, time
, and encoding/csv
🛠️. I started by defining the Thought
struct that represents a single thought, which includes the ID, content, and date created 🧱.
type Thought struct {
ID uuid.UUID
Content string
DateCreated time.Time
}
Then, I implemented the functions to interact with the CSV file 📁, such as adding a new thought, listing all thoughts, and deleting a thought.
Here is an example of how I added a new thought to the CSV file:
func addThought(thought Thought) error {
fileExists := false
if len(thought) < 1 {
fmt.Println("🤤 Please provide a thought")
return nil
}
if _, err := os.Stat(filepath); err == nil {
fileExists = true
}
file, err := os.OpenFile(filepath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
return err
}
defer file.Close()
newData := Thought{
ID: uuid.New(),
Content: thought,
DateCreated: time.Now(),
}
writer := csv.NewWriter(file)
if !fileExists {
// add headers to new csv file
writer.Write([]string{"ID", "Content", "DateCreated"})
}
thoughtData := []string{newData.ID.String(), newData.Content, newData.DateCreated.Format(time.RFC3339)}
writer.Write(thoughtData)
writer.Flush()
err = writer.Error()
if err != nil {
return err
}
fmt.Println("🤔 Thought added successfully")
return nil
}
Here is an example of how I listed all thoughts in the CSV file:
func listThoughts() ([]Thought, error) {
f, e := os.Open(filepath)
if e != nil {
return e
}
defer f.Close()
reader := csv.NewReader(f)
records, err := reader.ReadAll()
if err != nil {
return err
}
for i, r := range records {
if i != 0 {
fmt.Printf("%v: %v\n", i, r[1])
}
}
return nil
}
When working with files in Go, it's important to handle errors that may occur when opening, reading, or writing to a file. One common issue is forgetting to close the file after opening it, which can lead to resource leaks and potential data corruption. Another issue is not checking for errors when opening or writing to a file, which can result in unexpected program behavior.
Throughout the project, I learned how to work with Go's built-in data types, such as structs and time, as well as how to integrate external libraries into my code. Additionally, I gained experience working with CSV files and how to read and write to them using Go 📚.
github.com/google/uuid v1.3.0
: I chose to use this library to generate unique identifiers for the thoughts. UUIDs are a common identifier used in many applications, and I wanted to have a more universally recognized identifier for my thoughts.github.com/logrusorgru/aurora/v4
: I used this library to colorize the output to the terminal for a better user experience. By adding colors to the output, it makes it easier to read and understand the information being displayed.