{"id":804,"date":"2026-10-07T11:00:00","date_gmt":"2026-10-07T05:30:00","guid":{"rendered":"https:\/\/smitabharat.com\/blog\/go-function-programs\/"},"modified":"2026-11-29T01:29:34","modified_gmt":"2026-11-28T19:59:34","slug":"go-function-programs","status":"publish","type":"post","link":"https:\/\/smitabharat.com\/blog\/go-function-programs\/","title":{"rendered":"Go Function Programs with Output: Variadic, Closures, Defer"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Go function programs show how to break code into small, reusable pieces, which is the first step from short scripts to real applications. In this post, you will write fourteen function programs with their exact output, covering parameters, multiple return values, variadic functions, closures, functions as values and defer. Then you will practice the function questions that interviewers ask most, with real output for each one.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This post starts Level 4 of our Go programs series. You already used small functions in our <a href=\"https:\/\/smitabharat.com\/blog\/go-number-programs\/\">Go number programs<\/a> and <a href=\"https:\/\/smitabharat.com\/blog\/go-string-manipulation-programs\/\">Go string manipulation programs<\/a>. For the theory, read the <a href=\"https:\/\/smitabharat.com\/blog\/go-functions-beginners-guide\/\">Go functions guide for beginners<\/a>. All programs were tested on Go 1.27.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What are Go Function Programs?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Go function programs put a piece of logic behind a name, so you can call it as many times as you need. A function takes inputs called parameters, runs its body and can return one or more results.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Think of a function as a juice machine. You put in fruit (the parameters), the machine does its work (the body), and you get juice back (the return value). You do not need to know how the machine works inside to use it again and again.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Go Function Programs Matter<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Every Go program is built from functions, starting with <code>main<\/code>. For example, an API has a function per route, a billing system has a function for tax, and a test file has a function per test. So Go function programs teach you how to keep code short, readable and easy to test.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Reuse:<\/strong> write the logic once and call it from many places.<\/li>\n\n\n<li><strong>Testing:<\/strong> small functions are easy to test one by one.<\/li>\n\n\n<li><strong>Interviews:<\/strong> closures, variadic functions and defer order are popular questions.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">How Go Function Programs Work<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">When you call a function, Go copies each argument into the matching parameter. The body runs with those copies. When it reaches <code>return<\/code>, the results go back to the caller, and any deferred calls run just before the function ends.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-function-programs-diagram-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-function-programs-diagram-1-1024x640.png\" alt=\"Go function programs diagram showing arguments copied into parameters, the body running and results returned\" class=\"wp-image-789\" srcset=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-function-programs-diagram-1-1024x640.png 1024w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-function-programs-diagram-1-300x188.png 300w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-function-programs-diagram-1-768x480.png 768w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-function-programs-diagram-1-1536x960.png 1536w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-function-programs-diagram-1-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">Arguments are copied into parameters, the body runs, deferred calls run at the end, and results return to the caller.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The table below lists the function features in this post.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Feature<\/th><th>Syntax<\/th><th>Example use<\/th><\/tr><\/thead><tbody><tr><td>Parameters<\/td><td>func add(a, b int) int<\/td><td>Pass values in<\/td><\/tr><tr><td>Multiple returns<\/td><td>func div(a, b int) (int, error)<\/td><td>Return a result and an error<\/td><\/tr><tr><td>Named returns<\/td><td>func split(n int) (x, y int)<\/td><td>Self-documenting results<\/td><\/tr><tr><td>Variadic<\/td><td>func sum(nums &#8230;int) int<\/td><td>Any number of arguments<\/td><\/tr><tr><td>Function value<\/td><td>op := add<\/td><td>Store or pass a function<\/td><\/tr><tr><td>Closure<\/td><td>func() int { count++; return count }<\/td><td>Remember state between calls<\/td><\/tr><tr><td>defer<\/td><td>defer file.Close()<\/td><td>Run code when the function ends<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Basic Go Function Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These first Go function programs cover parameters and return values. If functions are new to you, start with Program 1.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 1: Your First Function That Says Hello<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This function takes one input and prints a message. It does not return anything.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ greet prints a hello message for the given name.\nfunc greet(name string) {\n    fmt.Println(\"Hello,\", name)\n}\n\nfunc main() {\n    \/\/ Call the same function three times with different names.\n    greet(\"Asha\")\n    greet(\"Ravi\")\n    greet(\"Meera\")\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Hello, Asha\nHello, Ravi\nHello, Meera<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The line <code>func greet(name string)<\/code> creates a function with one parameter called <code>name<\/code>. Each call in <code>main<\/code> passes a different name in the brackets. Then the body runs and prints the message. You wrote the print logic once, but you used it three times.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 2: A Simple Function with Parameters<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ area returns the area of a rectangle.\nfunc area(length, width float64) float64 { \/\/ both parameters are float64, and it returns a float64\n    return length * width\n}\n\nfunc main() {\n    fmt.Println(area(12.5, 4))\n    fmt.Println(area(3, 3))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>50\n9<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>length, width float64<\/code> means both parameters share the same type. The last <code>float64<\/code> is the type of the result. The <code>return<\/code> line sends the answer back, and <code>fmt.Println<\/code> prints it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 3: A Function That Answers Yes or No<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A function can return a <code>bool<\/code>, which is either true or false. You can use the answer straight inside an <code>if<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ isEven returns true when number divides by 2 with nothing left over.\nfunc isEven(number int) bool {\n    return number%2 == 0 \/\/ % gives the remainder after division\n}\n\nfunc main() {\n    for _, number := range []int{4, 7, 10} { \/\/ range gives each index (ignored with _) and value\n        if isEven(number) {\n            fmt.Println(number, \"is even\")\n        } else {\n            fmt.Println(number, \"is odd\")\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>4 is even\n7 is odd\n10 is even<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>number%2<\/code> is the remainder after dividing by 2. If the remainder is 0, the comparison is true, so the number is even. The loop calls <code>isEven<\/code> for each number and picks the right message.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 4: Multiple Return Values<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Go functions can return more than one value. This is how Go returns a result together with an error.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"errors\"\n    \"fmt\"\n)\n\n\/\/ divide returns the quotient, the remainder and an error.\nfunc divide(dividend, divisor int) (int, int, error) {\n    \/\/ Step 1: stop early if the divisor is zero.\n    if divisor == 0 {\n        return 0, 0, errors.New(\"cannot divide by zero\")\n    }\n    \/\/ Step 2: return both answers, and nil to mean \"no error\".\n    return dividend \/ divisor, dividend % divisor, nil\n}\n\nfunc main() {\n    \/\/ err holds the error value. It is nil when everything worked.\n    quotient, remainder, err := divide(17, 5)\n    fmt.Println(quotient, remainder, err)\n\n    \/\/ We only need the error here, so _ throws away the other two values.\n    _, _, err = divide(4, 0)\n    fmt.Println(err)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>3 2 &lt;nil&gt;\ncannot divide by zero<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This function lists three result types in brackets, so every <code>return<\/code> gives three values. The first call works, so <code>err<\/code> is nil and prints as <code>&lt;nil&gt;<\/code>. The blank identifier <code>_<\/code> throws away values you do not need. Here we only care about the error in the second call.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 5: Named Return Values<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ minMax returns the smallest and largest number in the slice.\nfunc minMax(numbers []int) (smallest, largest int) {\n    \/\/ Step 1: start with the first number for both.\n    smallest = numbers[0]\n    largest = numbers[0]\n    \/\/ Step 2: check the rest. numbers[1:] means \"from index 1 to the end\".\n    for _, number := range numbers[1:] {\n        if number &lt; smallest {\n            smallest = number\n        }\n        if number &gt; largest {\n            largest = number\n        }\n    }\n    return \/\/ a bare return sends back smallest and largest\n}\n\nfunc main() {\n    smallest, largest := minMax([]int{42, 7, 19, 88, 3})\n    fmt.Println(smallest, largest)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>3 88<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The results have names, <code>smallest<\/code> and <code>largest<\/code>, so they work like normal variables inside the function. Named results start at their zero values, and a bare <code>return<\/code> sends them back. Use this only in short functions, because it can hide what is being returned.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 6: Variadic Function<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A variadic function accepts any number of arguments of one type. Inside, they arrive as a slice.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ total prints how many prices there are and their sum.\nfunc total(label string, prices ...float64) { \/\/ ...float64 means \"any number of float64 values\"\n    sum := 0.0\n    for _, price := range prices { \/\/ prices is a normal slice here\n        sum += price\n    }\n    fmt.Printf(\"%s: %d items, total %.2f\\n\", label, len(prices), sum) \/\/ %.2f shows 2 decimals\n}\n\nfunc main() {\n    total(\"Cart A\", 99.5, 250, 45)\n    total(\"Cart B\") \/\/ no prices at all is allowed\n    cart := []float64{10, 20, 30}\n    total(\"Cart C\", cart...) \/\/ ... spreads the slice into separate arguments\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Cart A: 3 items, total 394.50\nCart B: 0 items, total 0.00\nCart C: 3 items, total 60.00<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Go collects all the extra arguments into the <code>prices<\/code> slice. With no prices, the slice is empty, so the total is 0. The variadic parameter must come last. To pass a slice you already have, add <code>...<\/code> after it, as in <code>cart...<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Function Values in Go Function Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In Go function programs, functions are values. You can store them in variables, pass them to other functions and return them.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 7: Pass a Function as an Argument<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ apply runs change on every number and returns the new numbers.\nfunc apply(numbers []int, change func(int) int) []int { \/\/ change is a function that takes and returns an int\n    result := make([]int, len(numbers)) \/\/ make creates a slice with room for every number\n    for i, number := range numbers {\n        result[i] = change(number)\n    }\n    return result\n}\n\n\/\/ square returns number times itself.\nfunc square(number int) int {\n    return number * number\n}\n\nfunc main() {\n    numbers := []int{1, 2, 3, 4}\n    fmt.Println(apply(numbers, square)) \/\/ pass square without () so it is not called yet\n\n    \/\/ A function with no name, written right here, that adds 10.\n    addTen := func(number int) int {\n        return number + 10\n    }\n    fmt.Println(apply(numbers, addTen))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[1 4 9 16]\n[11 12 13 14]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>apply<\/code> does not know what change it will make. It just calls whatever function you pass in. The first call passes <code>square<\/code>, and the second passes an anonymous function stored in <code>addTen<\/code>. This pattern appears all over the standard library, for example in <code>slices.SortFunc<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 8: A Map of Functions for a Calculator<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ add, subtract, multiply and divide each work on two numbers.\nfunc add(first, second float64) float64      { return first + second }\nfunc subtract(first, second float64) float64 { return first - second }\nfunc multiply(first, second float64) float64 { return first * second }\nfunc divide(first, second float64) float64   { return first \/ second }\n\nfunc main() {\n    \/\/ Each symbol (key) points to the function (value) that does the work.\n    operations := map[string]func(float64, float64) float64{\n        \"+\": add,\n        \"-\": subtract,\n        \"*\": multiply,\n        \"\/\": divide,\n    }\n    for _, symbol := range []string{\"+\", \"-\", \"*\", \"\/\"} {\n        operation := operations[symbol] \/\/ look up the function\n        result := operation(8, 2)       \/\/ call it\n        fmt.Printf(\"8 %s 2 = %g\\n\", symbol, result) \/\/ %g prints a number without extra zeros\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>8 + 2 = 10\n8 - 2 = 6\n8 * 2 = 16\n8 \/ 2 = 4<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The map links each symbol to a function. In the loop, we look up the function by its symbol and then call it with 8 and 2. Compare this with the switch version in our <a href=\"https:\/\/smitabharat.com\/blog\/go-switch-calculator-programs\/\">Go switch programs<\/a>. The map makes adding a new operator a one-line change.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 9: A Function That Returns a Function<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ multiplier returns a new function that multiplies by factor.\nfunc multiplier(factor int) func(int) int {\n    return func(number int) int {\n        return number * factor \/\/ the inner function remembers factor\n    }\n}\n\nfunc main() {\n    double := multiplier(2)\n    triple := multiplier(3)\n    fmt.Println(double(5), triple(5), double(triple(2)))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>10 15 12<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>multiplier<\/code> builds and returns a new function. Each returned function remembers its own <code>factor<\/code>, so <code>double<\/code> uses 2 and <code>triple<\/code> uses 3. In <code>double(triple(2))<\/code>, the inner call runs first and gives 6, then <code>double<\/code> turns it into 12.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Closures in Go Function Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In Go function programs, a closure is a function that remembers the variables around it, even after the outer function has finished.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 10: A Counter with a Closure<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ newCounter returns a function that counts up by one on each call.\nfunc newCounter() func() int {\n    count := 0 \/\/ this variable lives on inside the returned function\n    return func() int {\n        count++\n        return count\n    }\n}\n\nfunc main() {\n    first := newCounter()\n    second := newCounter()\n    fmt.Println(first(), first(), first())\n    fmt.Println(second(), second())\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>1 2 3\n1 2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Each call to <code>newCounter<\/code> makes a fresh <code>count<\/code> set to 0. The returned function keeps that variable and adds one on every call. <code>first<\/code> and <code>second<\/code> each have their own <code>count<\/code>. So calling <code>first<\/code> three times does not change <code>second<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 11: Fibonacci Generator with a Closure<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ fibGen returns a function that gives the next Fibonacci number on each call.\nfunc fibGen() func() int {\n    current, following := 0, 1\n    return func() int {\n        result := current\n        \/\/ Move one step: both values change at the same time.\n        current, following = following, current+following\n        return result\n    }\n}\n\nfunc main() {\n    next := fibGen()\n    for range 8 { \/\/ repeat 8 times (Go 1.22+)\n        fmt.Print(next(), \" \")\n    }\n    fmt.Println()\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>0 1 1 2 3 5 8 13<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The closure keeps <code>current<\/code> and <code>following<\/code> between calls. Each call returns <code>current<\/code>, then moves both values one step forward. Our <a href=\"https:\/\/smitabharat.com\/blog\/go-closures-anonymous-functions\/\">Go closures guide<\/a> explains how Go stores these variables.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">defer in Go Function Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>defer<\/code> schedules a call to run when the surrounding function returns. It is used to close files, release locks and log the end of a task.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 12: defer Runs in Reverse Order<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    fmt.Println(\"start\")\n    defer fmt.Println(\"first defer\")  \/\/ defer waits until main is about to end\n    defer fmt.Println(\"second defer\")\n    defer fmt.Println(\"third defer\")\n    fmt.Println(\"end\")\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>start\nend\nthird defer\nsecond defer\nfirst defer<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The normal lines print first, in order. When <code>main<\/code> ends, the deferred calls run. They run last in, first out, like a stack of plates.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 13: defer Reads Arguments Early<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    score := 10\n    defer fmt.Println(\"deferred score:\", score) \/\/ the value 10 is saved right now\n    score = 20\n    fmt.Println(\"current score:\", score)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>current score: 20\ndeferred score: 10<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Go evaluates the arguments of a deferred call at the moment you write <code>defer<\/code>. At that moment, <code>score<\/code> is 10. So the deferred line prints 10, not 20.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 14: Measure How Long a Function Takes<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"time\"\n)\n\n\/\/ timer notes the start time and returns a function that reports when the task ends.\nfunc timer(name string) func() {\n    start := time.Now()\n    return func() {\n        elapsed := time.Since(start) \/\/ time passed since start\n        fmt.Println(name, \"finished, took at least 50ms:\", elapsed &gt;= 50*time.Millisecond)\n    }\n}\n\n\/\/ slowTask waits for 50 milliseconds.\nfunc slowTask() {\n    defer timer(\"slowTask\")() \/\/ timer runs now, the returned function runs at the end\n    time.Sleep(50 * time.Millisecond)\n}\n\nfunc main() {\n    slowTask()\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>slowTask finished, took at least 50ms: true<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>timer(\"slowTask\")<\/code> runs at once and returns a closure. The extra <code>()<\/code> defers that closure, so it runs when <code>slowTask<\/code> ends. We print true or false instead of the exact time, so the output is the same on every run.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Value vs Pointer Parameters in Go Function Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In all Go function programs, Go passes a copy of each argument. The diagram and table below show what that means for values and pointers.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-function-programs-diagram-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-function-programs-diagram-2-1024x640.png\" alt=\"Go function programs comparison of value parameters and pointer parameters\" class=\"wp-image-790\" srcset=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-function-programs-diagram-2-1024x640.png 1024w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-function-programs-diagram-2-300x188.png 300w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-function-programs-diagram-2-768x480.png 768w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-function-programs-diagram-2-1536x960.png 1536w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-function-programs-diagram-2-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">A value parameter gets a copy, so changes stay inside, while a pointer parameter can change the caller&#8217;s data.<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Feature<\/th><th>Value parameter<\/th><th>Pointer parameter<\/th><\/tr><\/thead><tbody><tr><td>What is copied<\/td><td>The whole value<\/td><td>Only the address<\/td><\/tr><tr><td>Can change caller&#8217;s data<\/td><td>No<\/td><td>Yes<\/td><\/tr><tr><td>Cost for big structs<\/td><td>Copies everything<\/td><td>Cheap<\/td><\/tr><tr><td>Syntax<\/td><td>func f(u User)<\/td><td>func f(u *User)<\/td><\/tr><tr><td>Nil possible<\/td><td>No<\/td><td>Yes, check it<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">So pass values for small data you only read, and pointers when the function must change the data or the data is large. We cover this fully in the pointer programs post of this level.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common Mistakes in Go Function Programs<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Ignoring returned errors:<\/strong> always check the <code>error<\/code> result instead of using <code>_<\/code>.<\/li>\n\n\n<li><strong>Variadic not last:<\/strong> a <code>...<\/code> parameter must be the final one.<\/li>\n\n\n<li><strong>Forgetting &#8230; when passing a slice:<\/strong> <code>total(\"A\", cart)<\/code> does not compile; use <code>cart...<\/code>.<\/li>\n\n\n<li><strong>Expecting defer to see later changes:<\/strong> deferred arguments are read when <code>defer<\/code> runs.<\/li>\n\n\n<li><strong>Long functions:<\/strong> if a function needs a scroll bar, split it into smaller ones.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices for Go Function Programs<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Keep functions short<\/strong>, with one clear job each.<\/li>\n\n\n<li><strong>Return errors as the last result<\/strong>, which is the Go convention.<\/li>\n\n\n<li><strong>Use closures for small state<\/strong>, such as counters, but prefer structs for bigger state.<\/li>\n\n\n<li><strong>Use defer for cleanup<\/strong> right after you open a resource.<\/li>\n\n\n<li><strong>Name functions with verbs<\/strong>, such as <code>calculateTax<\/code> or <code>sendEmail<\/code>.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Go Function Programs Interview Questions and Answers<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Interviewers use short Go function programs to test rules that every developer should know. Each program is complete, with real output from Go 1.27.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Go Function Programs Parameter and Return Questions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q1. Does Go pass arguments by value or by reference?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Always by value. Go copies each argument. A slice, map or pointer is copied too, but the copy still points to the same underlying data, which is why changes through it are visible.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q2. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ update tries to change both of its inputs.\nfunc update(number int, scores []int) {\n    number = 100    \/\/ changes only the local copy\n    scores[0] = 100 \/\/ changes the shared array behind the slice\n}\n\nfunc main() {\n    number := 1\n    scores := []int{1, 2}\n    update(number, scores)\n    fmt.Println(number, scores)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>1 [100 2]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The int was copied, so the caller&#8217;s <code>number<\/code> stays 1. The slice header was copied too, but it points to the same array, so <code>scores[0]<\/code> changes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q3. Does Go support function overloading or default parameters?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">No. Each function name must be unique in a package, and there are no default values. Use different names, a variadic parameter or an options struct instead.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q4. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ swap returns its two inputs in the opposite order.\nfunc swap(first, second string) (string, string) {\n    return second, first\n}\n\nfunc main() {\n    left, right := swap(\"left\", \"right\")\n    fmt.Println(left, right)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>right left<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Closure and Defer Questions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q5. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    var squareFuncs []func() int \/\/ a slice that holds functions\n    for i := range 3 {\n        squareFuncs = append(squareFuncs, func() int { return i * i })\n    }\n    for _, getSquare := range squareFuncs {\n        fmt.Print(getSquare(), \" \")\n    }\n    fmt.Println()\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>0 1 4<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Since Go 1.22, each loop pass has its own <code>i<\/code>. So each closure remembers a different value.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q6. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ calc returns 5, but a deferred function doubles it on the way out.\nfunc calc() (result int) {\n    defer func() {\n        result *= 2 \/\/ runs after \"return 5\" has set result\n    }()\n    return 5\n}\n\nfunc main() {\n    fmt.Println(calc())\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>10<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>return 5<\/code> sets the named result to 5, then the deferred function runs and doubles it. A deferred function can change named results, which is a classic trick question.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q7. What does this program 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 func() { fmt.Print(i, \" \") }() \/\/ the () at the end calls the function (later, because of defer)\n    }\n    fmt.Print(\"loop done: \")\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>loop done: 2 1 0<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Each pass has its own <code>i<\/code>, and deferred calls run in reverse order. So we see 2, 1 and 0.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q8. What is an anonymous function?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is a function with no name, written inline. You can call it at once, as in <code>func() { fmt.Println(\"hi\") }()<\/code>, or store it in a variable.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    \/\/ Store a function with no name in a variable.\n    greet := func(name string) string {\n        return \"Hello, \" + name\n    }\n    fmt.Println(greet(\"Meera\"))\n\n    \/\/ Write a function and call it straight away with ().\n    func() {\n        fmt.Println(\"runs immediately\")\n    }()\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Hello, Meera\nruns immediately<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Type and Output Questions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q9. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    var addOne func(int) int \/\/ a function variable that is still empty (nil)\n    fmt.Println(addOne == nil)\n    addOne = func(number int) int {\n        return number + 1\n    }\n    fmt.Printf(\"%T %d\\n\", addOne, addOne(1)) \/\/ %T prints the type\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>true\nfunc(int) int 2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The zero value of a function variable is nil. Calling a nil function panics, so check it first when it might be unset.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q10. Can you compare two functions with ==?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">No. Functions can only be compared with nil. Writing <code>f == g<\/code> is a compile error.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q11. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ sum adds up any number of whole numbers.\nfunc sum(numbers ...int) int {\n    total := 0\n    for _, number := range numbers {\n        total += number\n    }\n    return total\n}\n\nfunc main() {\n    fmt.Println(sum(), sum(5), sum(1, 2, 3))\n    fmt.Println(sum([]int{10, 20}...))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>0 5 6\n30<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A variadic function can be called with zero, one or many arguments. With none, the slice is nil and the loop never runs.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q12. What is a higher-order function?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is a function that takes another function as an argument, or returns one. <code>apply<\/code> and <code>multiplier<\/code> in this post are both higher-order functions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q13. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    count := 1\n    increment := func() {\n        count++ \/\/ changes the real count in main\n    }\n    increment()\n    increment()\n    fmt.Println(count)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>3<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The closure uses the real <code>count<\/code>, not a copy. So each call changes the variable in <code>main<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Practice Task<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This task reuses two Go function programs from above. Write a function <code>makeDiscount(percent float64) func(float64) float64<\/code> that returns a function which applies the discount to a price. Create a 10% and a 25% discount and apply both to a price of 1,200. Hint: this is the same pattern as <code>multiplier<\/code> in Program 9.<\/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\/tour\/basics\/4\">A Tour of Go: Functions<\/a><\/li>\n\n\n<li><a href=\"https:\/\/go.dev\/ref\/spec#Function_declarations\">Go spec: function declarations<\/a><\/li>\n\n\n<li><a href=\"https:\/\/go.dev\/doc\/effective_go#functions\">Effective Go: functions<\/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 function programs take parameters, run a body and return zero or more results.<\/li>\n\n\n<li>Go passes every argument by value, but slices, maps and pointers share their data.<\/li>\n\n\n<li>Variadic functions take <code>...T<\/code> as the last parameter and receive a slice.<\/li>\n\n\n<li>Functions are values, so you can pass, store and return them.<\/li>\n\n\n<li>Closures remember outer variables, and <code>defer<\/code> runs calls in reverse order at the end.<\/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\">Next, functions start calling themselves. Read <a href=\"https:\/\/smitabharat.com\/blog\/go-recursion-programs\/\">Go recursion programs with output<\/a>, with factorial, power, GCD and Tower of Hanoi. For the interview angle, see the <a href=\"https:\/\/smitabharat.com\/blog\/go-functions-interview-questions-part-4\/\">Go functions interview questions<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Go function programs show how to break code into small, reusable pieces, which is the first step from short scripts to real applications. In&hellip;<\/p>\n","protected":false},"author":1,"featured_media":791,"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,28,95,11,104],"swapz_series":[],"class_list":["post-804","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-go-programs","tag-beginner","tag-go-functions","tag-go-interview-questions","tag-go-lang","tag-go-programs"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Go Function Programs: Variadic, Closures, Defer<\/title>\n<meta name=\"description\" content=\"Go function programs with output: multiple returns, variadic functions, function values, closures and defer, plus function interview questions.\" \/>\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-function-programs\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Go Function Programs: Variadic, Closures, Defer\" \/>\n<meta property=\"og:description\" content=\"Go function programs with output: multiple returns, variadic functions, function values, closures and defer, plus function interview questions.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/smitabharat.com\/blog\/go-function-programs\/\" \/>\n<meta property=\"og:site_name\" content=\"Smita Bharat - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-10-07T05:30:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-11-28T19:59:34+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-function-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-function-programs\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-function-programs\\\/\"},\"author\":{\"name\":\"Smita\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"headline\":\"Go Function Programs with Output: Variadic, Closures, Defer\",\"datePublished\":\"2026-10-07T05:30:00+00:00\",\"dateModified\":\"2026-11-28T19:59:34+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-function-programs\\\/\"},\"wordCount\":1970,\"publisher\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"image\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-function-programs\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-function-programs-featured.png\",\"keywords\":[\"Beginner\",\"Go Functions\",\"Go Interview Questions\",\"Go Lang\",\"Go Programs\"],\"articleSection\":[\"Go Programs\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-function-programs\\\/\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-function-programs\\\/\",\"name\":\"Go Function Programs: Variadic, Closures, Defer\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-function-programs\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-function-programs\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-function-programs-featured.png\",\"datePublished\":\"2026-10-07T05:30:00+00:00\",\"dateModified\":\"2026-11-28T19:59:34+00:00\",\"description\":\"Go function programs with output: multiple returns, variadic functions, function values, closures and defer, plus function interview questions.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-function-programs\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-function-programs\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-function-programs\\\/#primaryimage\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-function-programs-featured.png\",\"contentUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-function-programs-featured.png\",\"width\":2400,\"height\":1260,\"caption\":\"Go Function Programs featured image: variadic, closure and defer output\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-function-programs\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Go Function Programs with Output: Variadic, Closures, Defer\"}]},{\"@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 Function Programs: Variadic, Closures, Defer","description":"Go function programs with output: multiple returns, variadic functions, function values, closures and defer, plus function interview questions.","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-function-programs\/","og_locale":"en_US","og_type":"article","og_title":"Go Function Programs: Variadic, Closures, Defer","og_description":"Go function programs with output: multiple returns, variadic functions, function values, closures and defer, plus function interview questions.","og_url":"https:\/\/smitabharat.com\/blog\/go-function-programs\/","og_site_name":"Smita Bharat - Blog","article_published_time":"2026-10-07T05:30:00+00:00","article_modified_time":"2026-11-28T19:59:34+00:00","og_image":[{"width":2400,"height":1260,"url":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-function-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-function-programs\/#article","isPartOf":{"@id":"https:\/\/smitabharat.com\/blog\/go-function-programs\/"},"author":{"name":"Smita","@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"headline":"Go Function Programs with Output: Variadic, Closures, Defer","datePublished":"2026-10-07T05:30:00+00:00","dateModified":"2026-11-28T19:59:34+00:00","mainEntityOfPage":{"@id":"https:\/\/smitabharat.com\/blog\/go-function-programs\/"},"wordCount":1970,"publisher":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"image":{"@id":"https:\/\/smitabharat.com\/blog\/go-function-programs\/#primaryimage"},"thumbnailUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-function-programs-featured.png","keywords":["Beginner","Go Functions","Go Interview Questions","Go Lang","Go Programs"],"articleSection":["Go Programs"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/smitabharat.com\/blog\/go-function-programs\/","url":"https:\/\/smitabharat.com\/blog\/go-function-programs\/","name":"Go Function Programs: Variadic, Closures, Defer","isPartOf":{"@id":"https:\/\/smitabharat.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/smitabharat.com\/blog\/go-function-programs\/#primaryimage"},"image":{"@id":"https:\/\/smitabharat.com\/blog\/go-function-programs\/#primaryimage"},"thumbnailUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-function-programs-featured.png","datePublished":"2026-10-07T05:30:00+00:00","dateModified":"2026-11-28T19:59:34+00:00","description":"Go function programs with output: multiple returns, variadic functions, function values, closures and defer, plus function interview questions.","breadcrumb":{"@id":"https:\/\/smitabharat.com\/blog\/go-function-programs\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/smitabharat.com\/blog\/go-function-programs\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/smitabharat.com\/blog\/go-function-programs\/#primaryimage","url":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-function-programs-featured.png","contentUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-function-programs-featured.png","width":2400,"height":1260,"caption":"Go Function Programs featured image: variadic, closure and defer output"},{"@type":"BreadcrumbList","@id":"https:\/\/smitabharat.com\/blog\/go-function-programs\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/smitabharat.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Go Function Programs with Output: Variadic, Closures, Defer"}]},{"@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\/804","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=804"}],"version-history":[{"count":1,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/804\/revisions"}],"predecessor-version":[{"id":885,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/804\/revisions\/885"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/media\/791"}],"wp:attachment":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/media?parent=804"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/categories?post=804"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/tags?post=804"},{"taxonomy":"swapz_series","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/swapz_series?post=804"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}