{"id":838,"date":"2026-10-09T17:00:00","date_gmt":"2026-10-09T11:30:00","guid":{"rendered":"https:\/\/smitabharat.com\/blog\/go-tricky-output-programs\/"},"modified":"2026-11-29T01:29:26","modified_gmt":"2026-11-28T19:59:26","slug":"go-tricky-output-programs","status":"publish","type":"post","link":"https:\/\/smitabharat.com\/blog\/go-tricky-output-programs\/","title":{"rendered":"Go Tricky Output Programs: Defer, Closures, Slices and nil"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Go tricky output programs are short pieces of code where the printed result is not what most people expect. Interviewers love them because they show whether you really understand defer, closures, slices and interfaces. In this post, you will work through seventeen programs with their exact output, plus the logic explained in comments, so you can answer each one with confidence.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You should first finish our <a href=\"https:\/\/smitabharat.com\/blog\/go-function-programs\/\">Go function programs<\/a>, <a href=\"https:\/\/smitabharat.com\/blog\/go-interface-programs\/\">Go interface programs<\/a> and <a href=\"https:\/\/smitabharat.com\/blog\/go-generics-programs\/\">Go generics programs<\/a>. All programs were tested on Go 1.27.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What are Go Tricky Output Programs?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Go tricky output programs are small, complete programs that test one rule of the language. The code looks simple, but the answer depends on a detail, such as when a deferred call runs or whether two slices share memory.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Think of a magic trick. It looks impossible until someone shows you the hidden step. Each program here has one hidden step, and the comments show you where it is.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Go Tricky Output Programs Matter<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These rules are not just interview puzzles. Each one is the cause of a real bug that teams find in production.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Defer bugs:<\/strong> a value is read too early or a return value changes unexpectedly.<\/li>\n\n\n<li><strong>Slice bugs:<\/strong> one slice quietly changes another.<\/li>\n\n\n<li><strong>Nil bugs:<\/strong> an error that looks nil is not nil.<\/li>\n\n\n<li><strong>Interviews:<\/strong> almost every Go round has at least one &#8220;what does this print?&#8221; question.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">How to Solve Go Tricky Output Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Use the same three steps for every question. First, find the rule being tested. Next, trace the code line by line on paper, writing down each variable. Finally, write the output exactly, including spaces and new lines.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-tricky-output-programs-diagram-1-1024x640.png\" alt=\"Go tricky output programs diagram showing defer calls running in last in first out order\" class=\"wp-image-836\" srcset=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-tricky-output-programs-diagram-1-1024x640.png 1024w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-tricky-output-programs-diagram-1-300x188.png 300w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-tricky-output-programs-diagram-1-768x480.png 768w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-tricky-output-programs-diagram-1-1536x960.png 1536w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-tricky-output-programs-diagram-1-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Deferred calls are pushed onto a stack and run in reverse order when the function returns.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The table below lists the rules covered in this post.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Topic<\/th><th>Rule to remember<\/th><\/tr><\/thead><tbody><tr><td>defer<\/td><td>Runs last in, first out, and arguments are read at once<\/td><\/tr><tr><td>Named returns<\/td><td>A deferred function can change them<\/td><\/tr><tr><td>Loop variables<\/td><td>Each iteration has a new variable since Go 1.22<\/td><\/tr><tr><td>Slices<\/td><td>Share the array until append needs more space<\/td><\/tr><tr><td>Nil interface<\/td><td>A typed nil pointer inside an interface is not nil<\/td><\/tr><tr><td>Strings<\/td><td>len counts bytes, range walks runes<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Defer Go Tricky Output Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These first Go tricky output programs are all about <code>defer<\/code>, the most common topic in this kind of question. A <code>defer<\/code> line schedules a call to run when the current function ends.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 1: Order of Deferred Calls<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    defer fmt.Println(\"first\")  \/\/ pushed first, so it runs last\n    defer fmt.Println(\"second\")\n    defer fmt.Println(\"third\")  \/\/ pushed last, so it runs first\n    fmt.Println(\"main body\")\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>main body\nthird\nsecond\nfirst<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Deferred calls go onto a stack, like plates in a pile. First the normal line prints &#8220;main body&#8221;. When <code>main<\/code> returns, Go takes the calls off the top, so the last <code>defer<\/code> runs first.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 2: Defer Reads Arguments at Once<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    score := 1\n    defer fmt.Println(\"deferred score:\", score) \/\/ score is read NOW, so 1 is saved\n\n    defer func() {\n        fmt.Println(\"closure score:\", score) \/\/ the closure reads score LATER, at the end\n    }()\n\n    score = 2\n    fmt.Println(\"current score:\", score)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>current score: 2\nclosure score: 2\ndeferred score: 1<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The trick is when <code>score<\/code> is read. A deferred call reads its arguments when the <code>defer<\/code> line runs, so it saved 1. A closure is different, because it reads the variable when it finally runs, and by then <code>score<\/code> is 2.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 3: Defer Changes a Named Return<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ named has a named result: the defer can change result after return sets it.\nfunc named() (result int) {\n    defer func() {\n        result = result * 2 \/\/ runs after \"return 5\" has set result to 5\n    }()\n    return 5 \/\/ sets result = 5, then the defer makes it 10\n}\n\n\/\/ plain has an unnamed result: return copies the value out before the defer runs.\nfunc plain() int {\n    result := 5\n    defer func() {\n        result = result * 2 \/\/ changes the local variable only\n    }()\n    return result \/\/ the value 5 is already copied out\n}\n\nfunc main() {\n    fmt.Println(named(), plain())\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>10 5<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">With a named result, <code>return 5<\/code> first stores 5 in <code>result<\/code>. Then the deferred call runs and doubles it to 10. With a plain result, the value 5 is already copied out, so the change is lost.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 4: Recover Inside a Deferred Function<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ divide returns dividend \/ divisor, or an error instead of crashing.\nfunc divide(dividend, divisor int) (result int, err error) {\n    defer func() {\n        problem := recover() \/\/ recover only works inside a deferred call\n        if problem != nil {  \/\/ nil means there was no panic\n            err = fmt.Errorf(\"recovered: %v\", problem)\n        }\n    }()\n    return dividend \/ divisor, nil \/\/ panics when divisor is 0\n}\n\nfunc main() {\n    fmt.Println(divide(10, 2))\n    fmt.Println(divide(1, 0))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>5 &lt;nil&gt;\n0 recovered: runtime error: integer divide by zero<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Dividing by 0 causes a panic, which stops the function. Then the deferred call runs, catches the panic with <code>recover<\/code> and sets the named <code>err<\/code>. Because the panic happened before a value was returned, <code>result<\/code> keeps its zero value 0.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Closure and Loop Go Tricky Output Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Loops and closures were the source of a famous Go bug. Go 1.22 changed the rules, so these Go tricky output programs now give a friendlier answer.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 5: Closures Inside a Loop<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    var printers []func() \/\/ a slice of functions\n\n    for i := 0; i &lt; 3; i++ {\n        \/\/ Since Go 1.22, each iteration has its own i,\n        \/\/ so each closure keeps a different value.\n        printers = append(printers, func() {\n            fmt.Print(i, \" \")\n        })\n    }\n\n    for _, printNumber := range printers {\n        printNumber() \/\/ call each saved function\n    }\n    fmt.Println()\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>0 1 2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The first loop saves three small functions, and each one remembers its own <code>i<\/code>. Before Go 1.22, all three closures shared one <code>i<\/code>, and the program printed <code>3 3 3<\/code>. Now each iteration gets a fresh variable. The <code>go<\/code> line in your <code>go.mod<\/code> file decides which rule applies.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 6: Range over an Array Copies It<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    \/\/ Part 1: an array has a fixed size, written as [3]int.\n    numbersArray := [3]int{1, 2, 3}\n    for index, value := range numbersArray { \/\/ range loops over a COPY of the array\n        if index == 0 {\n            numbersArray[1] = 100 \/\/ change the original\n            numbersArray[2] = 200\n        }\n        fmt.Print(value, \" \")\n    }\n    fmt.Println(numbersArray)\n\n    \/\/ Part 2: a slice, written as []int, is not copied. It shares the data.\n    numbersSlice := []int{1, 2, 3}\n    for index, value := range numbersSlice {\n        if index == 0 {\n            numbersSlice[1] = 100\n            numbersSlice[2] = 200\n        }\n        fmt.Print(value, \" \")\n    }\n    fmt.Println(numbersSlice)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>1 2 3 [1 100 200]\n1 100 200 [1 100 200]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Both loops change items 1 and 2 on the first pass. An array is a value, so <code>range<\/code> loops over a copy and still prints the old 2 and 3. A slice points to shared memory, so the second loop sees the new values.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 7: Shadowing with :=<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    size := 1\n    if size := 2; size &gt; 1 { \/\/ := makes a NEW size that lives only inside the if\n        fmt.Println(\"inside:\", size)\n    }\n    fmt.Println(\"outside:\", size) \/\/ the outer size was never changed\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>inside: 2\noutside: 1<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>:=<\/code> always creates a new variable in the current block. So the <code>size<\/code> inside the <code>if<\/code> hides the outer one, and the outer one stays 1. This often hides an <code>err<\/code> by mistake, so read every <code>:=<\/code> inside an <code>if<\/code> carefully.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Slice Go Tricky Output Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Slices are the topic people get wrong most often in Go tricky output programs.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-tricky-output-programs-diagram-2-1024x640.png\" alt=\"Go tricky output programs diagram comparing append within capacity and append beyond capacity\" class=\"wp-image-837\" srcset=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-tricky-output-programs-diagram-2-1024x640.png 1024w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-tricky-output-programs-diagram-2-300x188.png 300w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-tricky-output-programs-diagram-2-768x480.png 768w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-tricky-output-programs-diagram-2-1536x960.png 1536w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-tricky-output-programs-diagram-2-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Within capacity, append writes into the shared array. Beyond capacity, Go copies to a new array.<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Program 8: Append Changes the Original Slice<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    a := []int{1, 2, 3, 4}\n    b := a[:2]        \/\/ b = [1 2], len 2, cap 4, same array as a\n    b = append(b, 99) \/\/ there is room, so 99 is written into a[2]\n    fmt.Println(a, b)\n    fmt.Println(len(b), cap(b)) \/\/ len is the item count, cap is the room\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[1 2 99 4] [1 2 99]\n3 4<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>a[:2]<\/code> takes the first two items, but <code>b<\/code> still uses the same array as <code>a<\/code>. Because <code>b<\/code> has spare capacity, <code>append<\/code> writes 99 into that shared array. That is why <code>a[2]<\/code> changed too.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 9: Append Beyond Capacity Makes a Copy<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    a := []int{1, 2, 3} \/\/ len 3, cap 3: no spare room\n    b := append(a, 4)   \/\/ Go must allocate a new, larger array\n    b[0] = 100          \/\/ only changes the new array\n    fmt.Println(a, b)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[1 2 3] [100 2 3 4]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This time <code>a<\/code> is full, so <code>append<\/code> copies everything to a new array for <code>b<\/code>. After that, changing <code>b[0]<\/code> does not touch <code>a<\/code>. To be safe, never rely on which case happens, and use <code>slices.Clone<\/code> when you need a real copy.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 10: copy Uses the Shorter Length<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    destination := make([]int, 2)               \/\/ room for exactly 2 items\n    copied := copy(destination, []int{1, 2, 3}) \/\/ copies only as many items as fit\n    fmt.Println(copied, destination)\n\n    var empty []int \/\/ a nil slice with length 0\n    fmt.Println(copy(empty, []int{7, 8})) \/\/ nothing fits in a nil slice\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>2 [1 2]\n0<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>copy<\/code> never grows the destination. It copies <code>min(len(dst), len(src))<\/code> items and returns that number. So only 2 items fit in the first case, and 0 fit in the empty slice.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 11: A Function Can Change Your Slice<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ doublePrices doubles every price in the slice it receives.\nfunc doublePrices(prices []int) {\n    for i := range prices {\n        prices[i] = prices[i] * 2 \/\/ writes into the caller's array\n    }\n}\n\n\/\/ doubleNumber doubles a plain int, which is a copy.\nfunc doubleNumber(number int) {\n    number = number * 2 \/\/ changes only the copy\n}\n\nfunc main() {\n    prices := []int{10, 20}\n    doublePrices(prices)\n    fmt.Println(prices)\n\n    count := 5\n    doubleNumber(count)\n    fmt.Println(count)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[20 40]\n5<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Go passes every argument as a copy. But a slice copy still points to the same array, so <code>doublePrices<\/code> changes the caller&#8217;s prices. An <code>int<\/code> copy has no link back, so <code>count<\/code> stays 5.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Interface and Type Go Tricky Output Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Interfaces and basic types hide some of the hardest Go tricky output programs, because the rules live deep in the language spec.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 12: The Nil Interface Trap<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ MyError is a custom error type.\ntype MyError struct{}\n\n\/\/ Error makes *MyError satisfy the error interface.\nfunc (*MyError) Error() string {\n    return \"my error\"\n}\n\n\/\/ find returns a typed nil pointer wrapped in an error interface.\nfunc find(found bool) error {\n    var problem *MyError \/\/ problem is nil, but its type is *MyError\n    if !found {\n        problem = &amp;MyError{}\n    }\n    return problem \/\/ the interface now holds (type=*MyError, value=nil)\n}\n\n\/\/ findFixed returns a real nil interface.\nfunc findFixed(found bool) error {\n    if !found {\n        return &amp;MyError{}\n    }\n    return nil \/\/ (type=nil, value=nil)\n}\n\nfunc main() {\n    fmt.Println(find(true) == nil)\n    fmt.Println(findFixed(true) == nil)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>false\ntrue<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">An interface is nil only when both its type and its value are nil. The first function returns a type with a nil value, so the check fails. Always return a plain <code>nil<\/code> for &#8220;no error&#8221;.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 13: Strings Count Bytes, Not Letters<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"unicode\/utf8\"\n)\n\nfunc main() {\n    word := \"h\u00e9llo\" \/\/ \u00e9 takes 2 bytes in UTF-8\n    fmt.Println(len(word), word[1], utf8.RuneCountInString(word))\n\n    for index, letter := range word { \/\/ range walks runes (letters), so index 2 is skipped\n        fmt.Printf(\"%d:%c \", index, letter) \/\/ %c prints a rune as a letter\n    }\n    fmt.Println()\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>6 195 5\n0:h 1:\u00e9 3:l 4:l 5:o<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>len<\/code> counts bytes, so &#8220;h\u00e9llo&#8221; has 6, not 5. Also <code>word[1]<\/code> is the first byte of &#8220;\u00e9&#8221;, which is 195. A <code>range<\/code> loop decodes whole runes instead, so the index jumps from 1 to 3.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 14: Integer Overflow and Division<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    var small int8 = 127 \/\/ int8 holds only -128 to 127\n    small++              \/\/ 127 + 1 does not fit, so it wraps around\n    fmt.Println(small)\n\n    fmt.Println(7\/2, -7\/2, -7%3) \/\/ integer division drops the fraction\n    fmt.Println(7.0 \/ 2)         \/\/ a float constant gives a float result\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>-128\n3 -3 -1\n3.5<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Go integers wrap around silently at runtime, so 127 + 1 becomes -128. Integer division rounds towards zero, and the remainder takes the sign of the first number. Once one side is a float, you get 3.5.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 15: switch Does Not Fall Through<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    number := 2\n    switch number {\n    case 1:\n        fmt.Println(\"one\")\n    case 2:\n        fmt.Println(\"two\")\n        fallthrough \/\/ runs the next case body without checking it\n    case 3:\n        fmt.Println(\"three\")\n    case 4:\n        fmt.Println(\"four\") \/\/ not reached: fallthrough moves only one step\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>two\nthree<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Go stops after the matching case by default, so you never need <code>break<\/code>. The <code>fallthrough<\/code> keyword runs only the very next case, without testing its condition. That is why &#8220;three&#8221; prints but &#8220;four&#8221; does not.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 16: fmt Prints a Map in Sorted Key Order<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    scores := map[string]int{\"ravi\": 2, \"asha\": 9, \"meera\": 5}\n\n    \/\/ fmt sorts the keys before printing a map, so this is always the same.\n    fmt.Println(scores)\n\n    \/\/ A range loop over a map has NO fixed order, so we only count here.\n    total := 0\n    for _, score := range scores {\n        total += score\n    }\n    fmt.Println(\"total:\", total)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>map[asha:9 meera:5 ravi:2]\ntotal: 16<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The trick is that two ways of reading a map behave differently. <code>fmt.Println<\/code> sorts the keys, so the printed map is always in a to z order. A <code>range<\/code> loop can visit keys in any order, so never print inside one if the order matters.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 17: Cannot Change a Struct Field Inside a Map<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Point is a position with one field.\ntype Point struct {\n    X int\n}\n\nfunc main() {\n    points := map[string]Point{\"home\": {X: 1}}\n    points[\"home\"].X = 5 \/\/ map values are not addressable, so this does not compile\n    fmt.Println(points)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>cannot assign to struct field points[\"home\"].X in map<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A map value is a copy, so Go refuses the change at compile time. Read the value, change it, then store it back with <code>home := points[\"home\"]; home.X = 5; points[\"home\"] = home<\/code>, or use <code>map[string]*Point<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Go Tricky Output Programs vs Normal Bugs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Both look like &#8220;the code prints the wrong thing&#8221;, but they come from different places. The table below compares them.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Feature<\/th><th>Tricky output<\/th><th>Normal bug<\/th><\/tr><\/thead><tbody><tr><td>Cause<\/td><td>A language rule you missed<\/td><td>A logic mistake in your code<\/td><\/tr><tr><td>Found by<\/td><td>Knowing the rule<\/td><td>Tests and debugging<\/td><\/tr><tr><td>Example<\/td><td>Typed nil in an interface<\/td><td>Wrong if condition<\/td><\/tr><tr><td>Fix<\/td><td>Change how you write the code<\/td><td>Change the logic<\/td><\/tr><tr><td>Prevent with<\/td><td>go vet, code review<\/td><td>Unit tests<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Common Mistakes in Go Tricky Output Programs<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Thinking defer arguments are read late:<\/strong> they are read when the <code>defer<\/code> line runs.<\/li>\n\n\n<li><strong>Expecting 3 3 3 from loop closures:<\/strong> that was the old rule before Go 1.22.<\/li>\n\n\n<li><strong>Treating a sub-slice as a copy:<\/strong> it shares the array until <code>append<\/code> grows it.<\/li>\n\n\n<li><strong>Returning a typed nil pointer as an error:<\/strong> return plain <code>nil<\/code> instead.<\/li>\n\n\n<li><strong>Using len to count letters:<\/strong> use <code>utf8.RuneCountInString<\/code>.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices for Go Tricky Output Programs<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Trace on paper<\/strong> and write down every variable after each line.<\/li>\n\n\n<li><strong>Write the output exactly<\/strong>, including spaces and blank lines.<\/li>\n\n\n<li><strong>Run go vet<\/strong> often, because it catches many of these traps.<\/li>\n\n\n<li><strong>Copy slices on purpose<\/strong> with <code>slices.Clone<\/code> when you need independent data.<\/li>\n\n\n<li><strong>Check the go.mod version<\/strong>, because loop rules depend on it.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Go Tricky Output Programs Interview Questions and Answers<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These are the output questions interviewers ask most, and they build on the Go tricky output programs above. Each code answer is complete, with real output from Go 1.27.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Output Questions on Go Tricky Output Programs<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q1. What does defer inside a loop print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    for i := 0; i &lt; 3; i++ {\n        defer fmt.Print(i, \" \") \/\/ i is read now: 0, then 1, then 2\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>2 1 0<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Each <code>defer<\/code> saves the current value of <code>i<\/code>, and the calls run in reverse order when <code>main<\/code> returns.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q2. What happens when you write to a nil map?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nfunc main() {\n    var ages map[string]int \/\/ nil map: reading is fine, writing is not\n    ages[\"asha\"] = 30\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>panic: assignment to entry in nil map<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Create the map with <code>make(map[string]int)<\/code> or a literal before writing to it. Reading from a nil map simply returns the zero value.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q3. What do you get from a closed channel?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    numbers := make(chan int, 1) \/\/ a channel with room for one value\n    numbers &lt;- 5\n    close(numbers)\n    fmt.Println(&lt;-numbers) \/\/ buffered values are still delivered\n\n    value, ok := &lt;-numbers \/\/ now empty and closed; ok is false\n    fmt.Println(value, ok)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>5\n0 false<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A closed channel still gives you any buffered values. After that, it returns the zero value and <code>ok<\/code> is false.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q4. Does assigning an array copy it?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    original := [2]int{1, 2}\n    duplicate := original \/\/ arrays are values, so duplicate is a full copy\n    duplicate[0] = 9\n    fmt.Println(original, duplicate)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[1 2] [9 2]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Arrays are copied on assignment, so changing <code>duplicate<\/code> leaves <code>original<\/code> alone. A slice assignment would share the data instead.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q5. Why does this method not change the struct?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Counter keeps a count.\ntype Counter struct {\n    count int\n}\n\n\/\/ Inc has a value receiver, so it works on a copy of the Counter.\nfunc (counter Counter) Inc() {\n    counter.count++\n}\n\nfunc main() {\n    visits := Counter{}\n    visits.Inc()\n    fmt.Println(visits.count)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>0<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A value receiver gets a copy, so the change is thrown away. Use <code>func (counter *Counter) Inc()<\/code> to change the original.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Nil and Type Questions in Go Tricky Output Programs<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q6. What is the difference between a nil slice and an empty slice?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    var nilSlice []int    \/\/ nil slice\n    emptySlice := []int{} \/\/ empty, but not nil\n    fmt.Println(nilSlice == nil, emptySlice == nil, len(nilSlice), len(emptySlice))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>true false 0 0<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Both have length 0 and both work with <code>append<\/code> and <code>range<\/code>. Only the nil slice is equal to <code>nil<\/code>, and JSON encodes it as <code>null<\/code> instead of <code>[]<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q7. What does this iota block print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nconst (\n    Small  = iota \/\/ 0\n    Medium        \/\/ 1\n    _             \/\/ 2 is skipped\n    Large         \/\/ 3\n)\n\nfunc main() {\n    fmt.Println(Small, Medium, Large)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>0 1 3<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>iota<\/code> starts at 0 and adds 1 on each line of the <code>const<\/code> block. The blank identifier <code>_<\/code> uses up a value without a name.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q8. Are two interfaces holding 1 and 1.0 equal?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    var whole any = 1     \/\/ type int\n    var decimal any = 1.0 \/\/ type float64\n    fmt.Println(whole == decimal)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>false<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Interface values are equal only when both the type and the value match. Here the types differ, so the answer is false.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q9. What happens when main sends on an unbuffered channel with no receiver?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nfunc main() {\n    numbers := make(chan int)\n    numbers &lt;- 1 \/\/ no goroutine is receiving, so main blocks forever\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>fatal error: all goroutines are asleep - deadlock!<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">An unbuffered send waits for a receiver. Go detects that no goroutine can ever run again and stops the program.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Concept Questions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q10. What changed for loop variables in Go 1.22?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Each iteration of a <code>for<\/code> loop now creates a new variable. Closures and goroutines started inside the loop see the value from their own iteration, which removes the old <code>i := i<\/code> workaround.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q11. When are deferred calls run if the function panics?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">They still run. As the panic moves up the stack, every deferred call runs in reverse order, and one of them can call <code>recover<\/code> to stop the panic.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q12. How do you prepare for tricky output questions?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Learn the six rules in the table at the top of this post, then practise Go tricky output programs by writing the output on paper before you run the code. Compare your answer with the real output and note any rule you missed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Practice Task<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To practise Go tricky output programs, predict the output of a function that returns a named <code>err<\/code>, defers a closure that wraps <code>err<\/code> with <code>fmt.Errorf<\/code>, and appends to a sub-slice of a slice passed in as an argument. Write your answer on paper first, then run it on the Go Playground and explain any difference.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Official Resources<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/go.dev\/ref\/spec\">The Go Programming Language Specification<\/a><\/li>\n\n\n<li><a href=\"https:\/\/go.dev\/blog\/defer-panic-and-recover\">Defer, Panic, and Recover on the Go blog<\/a><\/li>\n\n\n<li><a href=\"https:\/\/go.dev\/blog\/loopvar-preview\">Fixing For Loops in Go 1.22<\/a><\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Quick Recap<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Go tricky output programs each test one hidden rule of the language.<\/li>\n\n\n<li>Deferred calls run last in, first out, and their arguments are read at once.<\/li>\n\n\n<li>A deferred closure can change a named return value.<\/li>\n\n\n<li>Sub-slices share an array until <code>append<\/code> needs more room.<\/li>\n\n\n<li>A typed nil pointer inside an interface is not equal to nil.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You have finished Level 5 of the Go Programs series. Coming soon: Level 6, with data structure programs such as linked lists, stacks, queues and binary search. For more practice now, try the <a href=\"https:\/\/smitabharat.com\/blog\/go-tricky-output-questions-part-27\/\">Go tricky output interview questions<\/a> or revisit the <a href=\"https:\/\/smitabharat.com\/blog\/go-mutex-worker-pool-programs\/\">Go mutex and worker pool programs<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Go tricky output programs are short pieces of code where the printed result is not what most people expect. Interviewers love them because they&hellip;<\/p>\n","protected":false},"author":1,"featured_media":835,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_swapz_level":"","_swapz_series_part":0,"_swapz_hide_toc":false,"_swapz_hide_featured":false,"_swapz_description":"","footnotes":""},"categories":[106],"tags":[96,95,11,104,101],"swapz_series":[],"class_list":["post-838","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-go-programs","tag-beginner","tag-go-interview-questions","tag-go-lang","tag-go-programs","tag-go-tricky-questions"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Go Tricky Output Programs: Defer, Slices, nil<\/title>\n<meta name=\"description\" content=\"Go tricky output programs with answers: defer order, named returns, loop closures, slice append, nil interfaces and strings, plus interview Q&amp;A.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/smitabharat.com\/blog\/go-tricky-output-programs\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Go Tricky Output Programs: Defer, Slices, nil\" \/>\n<meta property=\"og:description\" content=\"Go tricky output programs with answers: defer order, named returns, loop closures, slice append, nil interfaces and strings, plus interview Q&amp;A.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/smitabharat.com\/blog\/go-tricky-output-programs\/\" \/>\n<meta property=\"og:site_name\" content=\"Smita Bharat - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-10-09T11:30:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-11-28T19:59:26+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-tricky-output-programs-featured.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2400\" \/>\n\t<meta property=\"og:image:height\" content=\"1260\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Smita\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Smita\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"16 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-tricky-output-programs\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-tricky-output-programs\\\/\"},\"author\":{\"name\":\"Smita\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"headline\":\"Go Tricky Output Programs: Defer, Closures, Slices and nil\",\"datePublished\":\"2026-10-09T11:30:00+00:00\",\"dateModified\":\"2026-11-28T19:59:26+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-tricky-output-programs\\\/\"},\"wordCount\":1981,\"publisher\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"image\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-tricky-output-programs\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-tricky-output-programs-featured.png\",\"keywords\":[\"Beginner\",\"Go Interview Questions\",\"Go Lang\",\"Go Programs\",\"Go Tricky Questions\"],\"articleSection\":[\"Go Programs\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-tricky-output-programs\\\/\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-tricky-output-programs\\\/\",\"name\":\"Go Tricky Output Programs: Defer, Slices, nil\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-tricky-output-programs\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-tricky-output-programs\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-tricky-output-programs-featured.png\",\"datePublished\":\"2026-10-09T11:30:00+00:00\",\"dateModified\":\"2026-11-28T19:59:26+00:00\",\"description\":\"Go tricky output programs with answers: defer order, named returns, loop closures, slice append, nil interfaces and strings, plus interview Q&A.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-tricky-output-programs\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-tricky-output-programs\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-tricky-output-programs\\\/#primaryimage\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-tricky-output-programs-featured.png\",\"contentUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-tricky-output-programs-featured.png\",\"width\":2400,\"height\":1260,\"caption\":\"Go Tricky Output Programs featured image: defer, slice and nil interface output\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-tricky-output-programs\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Go Tricky Output Programs: Defer, Closures, Slices and nil\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/\",\"name\":\"Smita Bharat Blog\",\"description\":\"Golang &amp; Backend Development Tutorials and Interview Questions\",\"publisher\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"alternateName\":\"Smita Bharat\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\",\"name\":\"Smita\",\"logo\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/smitabharat.com\\\/blog\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Go Tricky Output Programs: Defer, Slices, nil","description":"Go tricky output programs with answers: defer order, named returns, loop closures, slice append, nil interfaces and strings, plus interview Q&A.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/smitabharat.com\/blog\/go-tricky-output-programs\/","og_locale":"en_US","og_type":"article","og_title":"Go Tricky Output Programs: Defer, Slices, nil","og_description":"Go tricky output programs with answers: defer order, named returns, loop closures, slice append, nil interfaces and strings, plus interview Q&A.","og_url":"https:\/\/smitabharat.com\/blog\/go-tricky-output-programs\/","og_site_name":"Smita Bharat - Blog","article_published_time":"2026-10-09T11:30:00+00:00","article_modified_time":"2026-11-28T19:59:26+00:00","og_image":[{"width":2400,"height":1260,"url":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-tricky-output-programs-featured.png","type":"image\/png"}],"author":"Smita","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Smita","Est. reading time":"16 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/smitabharat.com\/blog\/go-tricky-output-programs\/#article","isPartOf":{"@id":"https:\/\/smitabharat.com\/blog\/go-tricky-output-programs\/"},"author":{"name":"Smita","@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"headline":"Go Tricky Output Programs: Defer, Closures, Slices and nil","datePublished":"2026-10-09T11:30:00+00:00","dateModified":"2026-11-28T19:59:26+00:00","mainEntityOfPage":{"@id":"https:\/\/smitabharat.com\/blog\/go-tricky-output-programs\/"},"wordCount":1981,"publisher":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"image":{"@id":"https:\/\/smitabharat.com\/blog\/go-tricky-output-programs\/#primaryimage"},"thumbnailUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-tricky-output-programs-featured.png","keywords":["Beginner","Go Interview Questions","Go Lang","Go Programs","Go Tricky Questions"],"articleSection":["Go Programs"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/smitabharat.com\/blog\/go-tricky-output-programs\/","url":"https:\/\/smitabharat.com\/blog\/go-tricky-output-programs\/","name":"Go Tricky Output Programs: Defer, Slices, nil","isPartOf":{"@id":"https:\/\/smitabharat.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/smitabharat.com\/blog\/go-tricky-output-programs\/#primaryimage"},"image":{"@id":"https:\/\/smitabharat.com\/blog\/go-tricky-output-programs\/#primaryimage"},"thumbnailUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-tricky-output-programs-featured.png","datePublished":"2026-10-09T11:30:00+00:00","dateModified":"2026-11-28T19:59:26+00:00","description":"Go tricky output programs with answers: defer order, named returns, loop closures, slice append, nil interfaces and strings, plus interview Q&A.","breadcrumb":{"@id":"https:\/\/smitabharat.com\/blog\/go-tricky-output-programs\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/smitabharat.com\/blog\/go-tricky-output-programs\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/smitabharat.com\/blog\/go-tricky-output-programs\/#primaryimage","url":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-tricky-output-programs-featured.png","contentUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-tricky-output-programs-featured.png","width":2400,"height":1260,"caption":"Go Tricky Output Programs featured image: defer, slice and nil interface output"},{"@type":"BreadcrumbList","@id":"https:\/\/smitabharat.com\/blog\/go-tricky-output-programs\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/smitabharat.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Go Tricky Output Programs: Defer, Closures, Slices and nil"}]},{"@type":"WebSite","@id":"https:\/\/smitabharat.com\/blog\/#website","url":"https:\/\/smitabharat.com\/blog\/","name":"Smita Bharat Blog","description":"Golang &amp; Backend Development Tutorials and Interview Questions","publisher":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"alternateName":"Smita Bharat","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/smitabharat.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339","name":"Smita","logo":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/image\/"},"sameAs":["https:\/\/smitabharat.com\/blog"]}]}},"_links":{"self":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/838","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/comments?post=838"}],"version-history":[{"count":1,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/838\/revisions"}],"predecessor-version":[{"id":898,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/838\/revisions\/898"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/media\/835"}],"wp:attachment":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/media?parent=838"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/categories?post=838"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/tags?post=838"},{"taxonomy":"swapz_series","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/swapz_series?post=838"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}