<?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: perez odiyo</title>
    <description>The latest articles on DEV Community by perez odiyo (@perezodiyo).</description>
    <link>https://dev.to/perezodiyo</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%2F3945422%2F496b7531-ce1b-4b5c-afb1-4e3e5a34bd5d.png</url>
      <title>DEV Community: perez odiyo</title>
      <link>https://dev.to/perezodiyo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/perezodiyo"/>
    <language>en</language>
    <item>
      <title>Go Beginner Mistake #1: When `log.Println()` Made Me Question `time.Now().Format()`</title>
      <dc:creator>perez odiyo</dc:creator>
      <pubDate>Fri, 05 Jun 2026 16:52:20 +0000</pubDate>
      <link>https://dev.to/perezodiyo/go-beginner-mistake-1-when-logprintln-made-me-question-timenowformat-3ig0</link>
      <guid>https://dev.to/perezodiyo/go-beginner-mistake-1-when-logprintln-made-me-question-timenowformat-3ig0</guid>
      <description>&lt;p&gt;&lt;em&gt;A small bug that taught me to read my tools before blaming my code.&lt;/em&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  The Plan
&lt;/h2&gt;

&lt;p&gt;I was building a &lt;strong&gt;Net-Cat project&lt;/strong&gt; in Go — a simplified version of the classic &lt;code&gt;nc&lt;/code&gt; (netcat) Unix tool. One of the features I wanted was a clean timestamp displayed whenever a client connected or sent a message. Something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;[2025-01-15 14:32:05] Alice has joined the chat
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Simple enough, right? I reached for &lt;code&gt;time.Now().Format()&lt;/code&gt; and felt good about it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;timestamp&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Now&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"2006-01-02 15:04:05"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"["&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;timestamp&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"] Alice has joined the chat"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I ran it, looked at the output, and... something was &lt;em&gt;off&lt;/em&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Confusing Output
&lt;/h2&gt;

&lt;p&gt;Instead of the clean line I expected, my terminal was showing this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;2025/01/15 14:32:05 [2025-01-15 14:32:05] Alice has joined the chat
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Two timestamps.&lt;/strong&gt; Right next to each other.&lt;/p&gt;

&lt;p&gt;My first thought? &lt;em&gt;My format string must be broken.&lt;/em&gt; I started Googling &lt;code&gt;time.Now().Format&lt;/code&gt; syntax. I tried different layouts. I rewrote the format string three times. Nothing changed. The double timestamp was still there, mocking me.&lt;/p&gt;

&lt;p&gt;I even briefly wondered if Go had some weird timezone bug. (It did not.)&lt;/p&gt;




&lt;h2&gt;
  
  
  The Culprit: &lt;code&gt;log.Println()&lt;/code&gt; vs &lt;code&gt;fmt.Println()&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Here's what I didn't know at the time:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;log.Println()&lt;/code&gt; and &lt;code&gt;fmt.Println()&lt;/code&gt; are NOT the same thing.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;fmt.Println()&lt;/code&gt; is simple. It prints exactly what you give it, adds a newline, and calls it a day.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello, world!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c"&gt;// Output: Hello, world!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;log.Println()&lt;/code&gt; is different. It's part of Go's &lt;code&gt;log&lt;/code&gt; package, which is designed for &lt;strong&gt;application logging&lt;/strong&gt; — the kind where you always want to know &lt;em&gt;when&lt;/em&gt; something happened. So by default, &lt;code&gt;log&lt;/code&gt; automatically prepends every message with the current date and time.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello, world!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c"&gt;// Output: 2025/01/15 14:32:05 Hello, world!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;See that prefix? You didn't ask for it. It's just... there. Always. That's the &lt;code&gt;log&lt;/code&gt; package doing its job.&lt;/p&gt;

&lt;p&gt;So when I wrote:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;timestamp&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Now&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"2006-01-02 15:04:05"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"["&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;timestamp&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"] Alice has joined the chat"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;log&lt;/code&gt; package saw my message, slapped its own timestamp at the front, and printed both. My code was working perfectly — I just didn't understand the tool I was using.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Does &lt;code&gt;log&lt;/code&gt; Do This?
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;log&lt;/code&gt; package in Go is built for one specific purpose: &lt;strong&gt;structured application logging&lt;/strong&gt;. When you're debugging a server that's been running for days, or tracing a crash in production, the absolute first thing you need to know is &lt;em&gt;when&lt;/em&gt; something happened.&lt;/p&gt;

&lt;p&gt;So the designers of the &lt;code&gt;log&lt;/code&gt; package made that the default. Every log entry gets a timestamp. You don't have to ask for it.&lt;/p&gt;

&lt;p&gt;You can actually control this behaviour with &lt;code&gt;log.SetFlags()&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="c"&gt;// Remove the automatic timestamp entirely&lt;/span&gt;
&lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SetFlags&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"No timestamp here!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c"&gt;// Output: No timestamp here!&lt;/span&gt;

&lt;span class="c"&gt;// Or keep just the time, not the date&lt;/span&gt;
&lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SetFlags&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Ltime&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Just the time"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c"&gt;// Output: 14:32:05 Just the time&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The default flag value is &lt;code&gt;log.LstdFlags&lt;/code&gt;, which equals &lt;code&gt;log.Ldate | log.Ltime&lt;/code&gt; — both date and time. That's the source of my double timestamp.&lt;/p&gt;




&lt;h2&gt;
  
  
  How I Finally Figured It Out
&lt;/h2&gt;

&lt;p&gt;After enough head-scratching, I stopped trying to fix my &lt;code&gt;time.Now().Format()&lt;/code&gt; code and started asking a different question: &lt;em&gt;"Where is this first timestamp actually coming from?"&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I removed my custom timestamp entirely and just logged a plain string:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Alice has joined the chat"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c"&gt;// Output: 2025/01/15 14:32:05 Alice has joined the chat&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There it was. The timestamp wasn't coming from my code at all. I opened the Go docs for the &lt;code&gt;log&lt;/code&gt; package for the first time, and the very first line told me everything:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;"Package log implements a simple logging package. Each logging operation makes a single call to the Writer's Write method. A Logger can be used simultaneously from multiple goroutines; it guarantees serialized access to the Writer. The default logger writes to standard error and prints the date and time of each logged message."&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Date and time of each logged message.&lt;/strong&gt; Right there in the first paragraph. I just hadn't read it.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Fix
&lt;/h2&gt;

&lt;p&gt;For my Net-Cat project, I had two clean options:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Option 1:&lt;/strong&gt; Switch to &lt;code&gt;fmt.Println()&lt;/code&gt; and keep full control of formatting.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;timestamp&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Now&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"2006-01-02 15:04:05"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"["&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;timestamp&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"] Alice has joined the chat"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c"&gt;// Output: [2025-01-15 14:32:05] Alice has joined the chat&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Option 2:&lt;/strong&gt; Keep &lt;code&gt;log.Println()&lt;/code&gt; but disable its automatic prefix.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SetFlags&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// Call this once at the start of your program&lt;/span&gt;
&lt;span class="n"&gt;timestamp&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Now&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"2006-01-02 15:04:05"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"["&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;timestamp&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"] Alice has joined the chat"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c"&gt;// Output: [2025-01-15 14:32:05] Alice has joined the chat&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I went with Option 1. &lt;code&gt;fmt&lt;/code&gt; was the right tool for user-facing chat messages; &lt;code&gt;log&lt;/code&gt; is better suited for actual application diagnostics.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Real Lesson
&lt;/h2&gt;

&lt;p&gt;The bug was never in my &lt;code&gt;time.Now().Format()&lt;/code&gt; code. It was in my assumption that &lt;code&gt;log.Println()&lt;/code&gt; behaved like &lt;code&gt;fmt.Println()&lt;/code&gt;. They share a similar name and do similar things on the surface, but they're built for completely different jobs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The takeaway for new Go developers:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;When something isn't working, before you rewrite your logic — make sure you understand every tool you're using.&lt;/strong&gt; A quick trip to the official Go docs (&lt;a href="https://pkg.go.dev" rel="noopener noreferrer"&gt;pkg.go.dev&lt;/a&gt;) can save you an hour of chasing the wrong bug.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Use &lt;code&gt;fmt&lt;/code&gt; when you're talking &lt;strong&gt;to the user&lt;/strong&gt;.&lt;br&gt;
Use &lt;code&gt;log&lt;/code&gt; when you're talking &lt;strong&gt;to yourself&lt;/strong&gt; (or your future debugging self).&lt;/p&gt;

&lt;p&gt;And when output looks weird? Don't assume your code is wrong. Ask where &lt;em&gt;every part&lt;/em&gt; of that output is coming from.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Happy coding — and may your timestamps never double up again.&lt;/em&gt; 🕐&lt;/p&gt;

</description>
      <category>go</category>
      <category>beginners</category>
      <category>debugging</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
