<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sadia Ijaz</title>
    <description>The latest articles on DEV Community by Sadia Ijaz (@devsadia).</description>
    <link>https://dev.to/devsadia</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2622115%2F8115bcd7-cdfe-4a09-b2fb-72cdf31c1845.png</url>
      <title>DEV Community: Sadia Ijaz</title>
      <link>https://dev.to/devsadia</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/devsadia"/>
    <language>en</language>
    <item>
      <title>Building a CLI Task Tracker with Go: My Learning Journey</title>
      <dc:creator>Sadia Ijaz</dc:creator>
      <pubDate>Fri, 27 Dec 2024 15:38:24 +0000</pubDate>
      <link>https://dev.to/devsadia/building-a-cli-task-tracker-with-go-my-learning-journey-259h</link>
      <guid>https://dev.to/devsadia/building-a-cli-task-tracker-with-go-my-learning-journey-259h</guid>
      <description>&lt;p&gt;Learning a new programming language is an opportunity to make mistakes, learn from them, and grow as a developer. Recently, I took on the challenge of learning Go (Golang) by building my first project: a CLI Task Tracker suggested by roadmap.sh.&lt;/p&gt;

&lt;p&gt;This article highlights the mistakes I made, how I resolved them, and the valuable lessons I learned along the way.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Why Go and a CLI Task Tracker?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;When learning Go, I wanted to work on a project that would be practical yet simple enough to grasp the basics. The CLI Task Tracker idea came from roadmap.sh, which suggested it as a beginner-friendly project.&lt;/p&gt;

&lt;p&gt;I started learning Go through Hitesh Choudhary’s YouTube tutorials, which gave me a good foundation. Wanting to dive deeper, I turned to FreeCodeCamp’s resources, but they felt overwhelming and less beginner-friendly for my skill level at that time. Frustrated, I decided to tackle Go on my own and started working on the task tracker project. This hands-on approach forced me to learn through trial and error, confronting challenges head-on.&lt;/p&gt;

&lt;p&gt;**&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a CLI?
&lt;/h2&gt;

&lt;p&gt;**&lt;br&gt;
Before starting this project, I didn’t even know what a CLI (Command Line Interface) was. My experience was limited to GUIs (Graphical User Interfaces). During my research, I discovered that a CLI allows users to interact with their computer using text commands, offering a lightweight yet powerful way to execute tasks.&lt;/p&gt;

&lt;p&gt;Initially, I found the idea of using a CLI intimidating. However, as I learned about its structure and experimented with basic commands, it started to feel much simpler. Understanding the power of CLIs gave me the confidence to move forward with the project.&lt;/p&gt;

&lt;p&gt;**&lt;/p&gt;

&lt;h2&gt;
  
  
  Building the Task Tracker: Mistakes and Learning Moments
&lt;/h2&gt;

&lt;p&gt;**&lt;br&gt;
&lt;strong&gt;1. Using fmt.Scanln for Input&lt;/strong&gt;&lt;br&gt;
One of my first mistakes was using fmt.Scanln to gather user input. While it works for basic tasks, it’s not the right approach for building a CLI application, as it’s not designed for parsing multiple arguments or handling command-line commands efficiently.&lt;br&gt;
Here’s an example of my early code:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;fmt.Println("Enter Your First Name: ")&lt;br&gt;
var first string&lt;br&gt;
fmt.Scanln(&amp;amp;first)&lt;br&gt;
fmt.Println("Enter Your Last Name: ")&lt;br&gt;
var last string&lt;br&gt;
fmt.Scanln(&amp;amp;last)&lt;br&gt;
fmt.Println("Hello " + first + " " + last)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This approach is fine for learning input handling but completely unsuitable for a CLI tool. I soon realized that I needed to parse arguments passed via the command line rather than relying on runtime input.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Misusing the Deprecated ioutil Package&lt;/strong&gt;&lt;br&gt;
Another significant mistake was attempting to use the ioutil package for file operations. While researching Go’s standard library, I came across examples using ioutil, only to later discover that it had been deprecated. This meant I was using outdated and discouraged practices.&lt;br&gt;
For example, I initially tried this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;data, err := ioutil.ReadFile("tasks.txt")&lt;br&gt;
if err != nil {&lt;br&gt;
 fmt.Println("Error reading file:", err)&lt;br&gt;
}&lt;br&gt;
fmt.Println(string(data))&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The correct approach was to switch to the newer os and io packages, which provide better functionality and are actively maintained. I replaced the above code with:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;file, err := os.Open("tasks.txt")&lt;br&gt;
if err != nil {&lt;br&gt;
 fmt.Println("Error opening file:", err)&lt;br&gt;
 return&lt;br&gt;
}&lt;br&gt;
defer file.Close()&lt;br&gt;
data, err := io.ReadAll(file)&lt;br&gt;
if err != nil {&lt;br&gt;
 fmt.Println("Error reading file:", err)&lt;br&gt;
}&lt;br&gt;
fmt.Println(string(data))&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This mistake taught me the importance of checking documentation for updates before using a library or package.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;. Sticking to os.Args Instead of Libraries&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I decided to stick to Go’s standard library and used os.Args for parsing command-line arguments. While this approach works, it can become messy and hard to maintain as the application grows.&lt;/p&gt;

&lt;p&gt;Here’s an example of how I used os.Args:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;if len(os.Args) &amp;lt; 2 {&lt;br&gt;
 fmt.Println("Usage: task-cli &amp;lt;command&amp;gt; [arguments]")&lt;br&gt;
 return&lt;br&gt;
}&lt;br&gt;
command := os.Args[1]&lt;br&gt;
switch command {&lt;br&gt;
case "add":&lt;br&gt;
 fmt.Println("Adding a task…")&lt;br&gt;
case "delete":&lt;br&gt;
 fmt.Println("Deleting a task…")&lt;br&gt;
default:&lt;br&gt;
 fmt.Println("Unknown command:", command)&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How I Sought Help&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There were many times when I got stuck. To move forward, I turned to several resources:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Stack Overflow&lt;/em&gt;&lt;/strong&gt;: I found answers to specific problems, such as parsing command-line arguments or handling file storage.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;ChatGPT:&lt;/em&gt;&lt;/strong&gt; I used AI to clarify concepts and find examples that were beginner-friendly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Documentation:&lt;/em&gt;&lt;/strong&gt; Reading the official Go documentation was invaluable, especially when I discovered that ioutil was deprecated and learned about alternatives like os and io.&lt;/p&gt;

&lt;p&gt;This combination of resources helped me push through challenges and continue building my project.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Learning from Others&lt;/strong&gt;&lt;br&gt;
After completing my version of the task tracker, I decided to explore how other developers approached similar projects. I found that some solutions were similar to mine. However, others used more advanced tools like the cmd package, which provides a structured way to manage CLI commands.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxtad0z8lsn6iqnlp7oc1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxtad0z8lsn6iqnlp7oc1.png" alt="Image description" width="800" height="364"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;While I don’t fully understand cmd yet, seeing its use has motivated me to learn more about Go's ecosystem and explore advanced techniques in future projects.&lt;/p&gt;

&lt;p&gt;I chose not to use libraries like cobra or flag, which would have provided a more structured way to handle commands and arguments. While this was a deliberate decision to focus on learning Go's basics, I now realize how much easier those libraries would have made the development process.&lt;/p&gt;

&lt;p&gt;**&lt;/p&gt;

&lt;h3&gt;
  
  
  Project Repo
&lt;/h3&gt;

&lt;p&gt;**&lt;br&gt;
GitHub: task-tracker-cli&lt;/p&gt;

&lt;p&gt;**&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Learnings
&lt;/h2&gt;

&lt;p&gt;**&lt;/p&gt;

&lt;p&gt;Mistakes Are Part of Learning: My mistakes, whether it was using fmt.Scanln or relying on ioutil, were valuable lessons. Each one helped me understand the right tools and practices for building robust CLI applications.&lt;br&gt;
Stay Updated on Libraries and Best Practices: The ioutil package being deprecated was a wake-up call to always check the latest documentation and follow best practices to avoid outdated methods.&lt;br&gt;
Start with the Basics Before Exploring Libraries: While I didn’t use advanced libraries like cobra, understanding the basics of os.Args and Go's standard library gave me a solid foundation. Now, I feel ready to explore these libraries in future projects.&lt;br&gt;
Experiment and Compare Solutions: Looking at how others solved similar problems introduced me to tools and techniques I hadn’t considered. For example, I learned about flag and cobra through other developers' implementations, which motivated me to explore them.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Conclusion&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Building a CLI Task Tracker in Go was an eye-opening experience. It was far from perfect, but it taught me the importance of starting small, making mistakes, and learning from them.&lt;/p&gt;

&lt;p&gt;If you’re new to Go or CLI tools, my advice is simple: don’t be afraid to experiment, even if it means making a few mistakes. Each misstep is a chance to learn and improve. As for me, I’m excited to explore more of Go’s ecosystem and build on this foundation.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Connect with Me:&lt;br&gt;
If you’d like to see more of my projects or share your feedback, feel free to connect with me:&lt;br&gt;
GitHub: github.com/girldocode&lt;br&gt;
Twitter: x.com/girldocode&lt;/p&gt;
&lt;/blockquote&gt;

</description>
    </item>
  </channel>
</rss>
