{"id":508,"date":"2018-11-07T11:20:31","date_gmt":"2018-11-07T19:20:31","guid":{"rendered":"http:\/\/www.nathanbak.com\/?p=508"},"modified":"2026-01-02T20:05:20","modified_gmt":"2026-01-03T04:05:20","slug":"redirecting-stdout-to-dev-null-in-go","status":"publish","type":"post","link":"https:\/\/nathanbak.com\/?p=508","title":{"rendered":"Redirecting stdout to \/dev\/null in Go"},"content":{"rendered":"<p>Yesterday was the six month anniversary since I started my new job.\u00a0 It&#8217;s been busy and I&#8217;ve learned a lot.\u00a0 One thing I had to start learning was the Go programming language (aka Golang).\u00a0 Today I wanted to temporarily suppress the standard out, but searching the Internet didn&#8217;t come up with any concise solutions.\u00a0 What I did was pretty simple, but seems to work.<\/p>\n<p>Basically, I&#8217;m using a third party package to parse an ini file.\u00a0 The code works fine, but it is very verbose and prints out several lines of information that end up cluttering my logs.\u00a0 My solution was to simply redirect the standard out to \/dev\/null.\u00a0 Here&#8217;s what my code ended up looking like:<\/p>\n<pre>func unmarshal(bytes []byte, info *InfoStruct) error {\r\n\r\n    \/\/ Redirect standard out to null\r\n    stdout := os.Stdout\r\n    defer func() { os.Stdout = stdout }()\r\n    os.Stdout = os.NewFile(0, os.DevNull)\r\n\r\n    return ini.Unmarshal(bytes, info)\r\n}<\/pre>\n<p>Hopefully the code is easy to follow.\u00a0 The &#8220;InfoStruct&#8221; is just some struct for data that is in the ini file.\u00a0 The &#8220;ini&#8221; prefix is for the third party package that I am using.<\/p>\n<p>In my quick searching, I found various information about redirecting logs, redirecting command output, etc.\u00a0 There were even some examples of redirecting the stdout, but none were exactly what I wanted and most were less concise.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Yesterday was the six month anniversary since I started my new job.\u00a0 It&#8217;s been busy and I&#8217;ve learned a lot.\u00a0 One thing I had to start learning was the Go programming language (aka Golang).\u00a0 Today I wanted to temporarily suppress the standard out, but searching the Internet didn&#8217;t come up with any concise solutions.\u00a0 What &hellip; <a href=\"https:\/\/nathanbak.com\/?p=508\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Redirecting stdout to \/dev\/null in Go&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[19],"class_list":["post-508","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-golang"],"_links":{"self":[{"href":"https:\/\/nathanbak.com\/index.php?rest_route=\/wp\/v2\/posts\/508","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nathanbak.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nathanbak.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nathanbak.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nathanbak.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=508"}],"version-history":[{"count":2,"href":"https:\/\/nathanbak.com\/index.php?rest_route=\/wp\/v2\/posts\/508\/revisions"}],"predecessor-version":[{"id":510,"href":"https:\/\/nathanbak.com\/index.php?rest_route=\/wp\/v2\/posts\/508\/revisions\/510"}],"wp:attachment":[{"href":"https:\/\/nathanbak.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=508"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nathanbak.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=508"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nathanbak.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=508"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}