{"id":165,"date":"2026-07-07T20:29:23","date_gmt":"2026-07-07T20:29:23","guid":{"rendered":"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/06-go-functions\/"},"modified":"2026-07-07T20:38:10","modified_gmt":"2026-07-07T20:38:10","slug":"06-go-functions","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/06-go-functions\/","title":{"rendered":"Go Functions \u2013 Declarations, Returns, and defer"},"content":{"rendered":"<p><!-- ktg-updated-banner --><\/p>\n<div class=\"ktg-updated-banner\" style=\"margin:0 0 1.25em;padding:0.75em 1em;background:#fefce8;border-left:4px solid #ca8a04;border-radius:4px;\">\n<p><strong>Updated July 2, 2026.<\/strong> Refreshed for Go 1.22+ and current SEO best practices.<\/p>\n<\/div>\n<p>In this lesson we cover the <strong>Go functions tutorial<\/strong> fundamentals \u2014 how to declare functions, return multiple values, use named return parameters, accept a variable number of arguments, and schedule cleanup with <code>defer<\/code>. If you completed <a href=\"https:\/\/www.kindsonthegenius.com\/go\/05-go-control-flow\/\">Lesson 5 \u2013 Control Flow<\/a>, you can branch and loop; functions let you package that logic into reusable, testable units.<\/p>\n<p><strong>Prerequisites:<\/strong> Lessons 1\u20135, especially <a href=\"https:\/\/www.kindsonthegenius.com\/go\/04-go-variables-and-types\/\">Variables and Types<\/a> and <a href=\"https:\/\/www.kindsonthegenius.com\/go\/05-go-control-flow\/\">Control Flow<\/a>. <strong>Estimated time:<\/strong> 45\u201355 minutes.<\/p>\n<h4 id=\"declarations\">1. Function Declarations<\/h4>\n<p>A function is declared with the <code>func<\/code> keyword, a name, parameters in parentheses, and an optional return type:<\/p>\n<pre><code class=\"language-go\">package main\n\nimport \"fmt\"\n\nfunc greet(name string) {\n\tfmt.Printf(\"Hello, %s!\\n\", name)\n}\n\nfunc add(a int, b int) int {\n\treturn a + b\n}\n\nfunc main() {\n\tgreet(\"Kindson\")\n\tfmt.Println(\"3 + 4 =\", add(3, 4))\n}\n<\/code><\/pre>\n<p>When consecutive parameters share the same type, you can write <code>a, b int<\/code> instead of <code>a int, b int<\/code>. Functions are first-class values in Go \u2014 you can assign them to variables and pass them as arguments \u2014 but start with named functions at package level while learning. Names starting with an uppercase letter are exported (visible outside the package); lowercase names are private.<\/p>\n<h4 id=\"multiple-returns\">2. Multiple Return Values<\/h4>\n<p>Go functions can return more than one value. This pattern is central to error handling \u2014 a result plus an <code>error<\/code>:<\/p>\n<pre><code class=\"language-go\">package main\n\nimport (\n\t\"errors\"\n\t\"fmt\"\n)\n\nfunc divide(a, b float64) (float64, error) {\n\tif b == 0 {\n\t\treturn 0, errors.New(\"division by zero\")\n\t}\n\treturn a \/ b, nil\n}\n\nfunc main() {\n\tresult, err := divide(10, 2)\n\tif err != nil {\n\t\tfmt.Println(\"Error:\", err)\n\t\treturn\n\t}\n\tfmt.Println(\"Result:\", result)\n}\n<\/code><\/pre>\n<p>Callers must handle every return value or explicitly discard with <code>_<\/code>. Ignoring an error return is allowed by the compiler but is poor practice in production code. This explicit style differs from exceptions in languages like Java or Python \u2014 errors are values you check at the call site.<\/p>\n<h4 id=\"named-returns\">3. Named Return Values<\/h4>\n<p>You can name return parameters in the function signature. They act as variables inside the function, and a bare <code>return<\/code> statement returns their current values:<\/p>\n<pre><code class=\"language-go\">package main\n\nimport \"fmt\"\n\nfunc rectangleStats(width, height float64) (area, perimeter float64) {\n\tarea = width * height\n\tperimeter = 2 * (width + height)\n\treturn \/\/ naked return \u2014 returns area and perimeter\n}\n\nfunc main() {\n\ta, p := rectangleStats(5, 3)\n\tfmt.Printf(\"Area: %.1f, Perimeter: %.1f\\n\", a, p)\n}\n<\/code><\/pre>\n<p>Named returns improve readability in short functions with obvious results, but overuse in long functions can obscure what is being returned. The Go community often reserves naked returns for very small functions \u2014 defer and error cleanup patterns are a common exception you will see in production code.<\/p>\n<h4 id=\"variadic\">4. Variadic Functions<\/h4>\n<p>A <strong>variadic<\/strong> function accepts zero or more arguments of a specified type. The parameter is declared with an ellipsis before the type and arrives inside the function as a slice:<\/p>\n<pre><code class=\"language-go\">package main\n\nimport \"fmt\"\n\nfunc sum(numbers ...int) int {\n\ttotal := 0\n\tfor _, n := range numbers {\n\t\ttotal += n\n\t}\n\treturn total\n}\n\nfunc main() {\n\tfmt.Println(sum(1, 2, 3))       \/\/ 6\n\tfmt.Println(sum(10, 20))        \/\/ 30\n\tfmt.Println(sum())              \/\/ 0\n\n\tvals := []int{4, 5, 6}\n\tfmt.Println(sum(vals...))       \/\/ spread a slice\n}\n<\/code><\/pre>\n<p>Only the last parameter in a function can be variadic. Built-in examples include <code>fmt.Println<\/code> and <code>append<\/code>. Use variadic functions when the number of arguments is naturally variable \u2014 logging, formatting, or aggregating values \u2014 rather than forcing callers to always build a slice manually.<\/p>\n<h4 id=\"defer\">5. Introduction to defer<\/h4>\n<p>The <code>defer<\/code> statement schedules a function call to run when the surrounding function returns \u2014 regardless of how it returns (normal return, early return, or panic):<\/p>\n<pre><code class=\"language-go\">package main\n\nimport \"fmt\"\n\nfunc process() {\n\tfmt.Println(\"Start\")\n\tdefer fmt.Println(\"Deferred cleanup\")\n\tfmt.Println(\"Middle\")\n\tfmt.Println(\"End\")\n}\n\nfunc main() {\n\tprocess()\n}\n\/\/ Output: Start, Middle, End, Deferred cleanup\n<\/code><\/pre>\n<p>Deferred calls run in <strong>LIFO<\/strong> order (last deferred, first executed). Typical uses: closing files, unlocking mutexes, and rolling back transactions. Pair <code>defer file.Close()<\/code> immediately after a successful <code>os.Open<\/code> so cleanup is never forgotten on an error path \u2014 a pattern you will use constantly in Go.<\/p>\n<h4 id=\"patterns\">6. Putting Functions Together<\/h4>\n<p>Real Go code combines these features. Here is a small utility that demonstrates parameters, multiple returns, and defer:<\/p>\n<pre><code class=\"language-go\">package main\n\nimport (\n\t\"fmt\"\n\t\"strings\"\n)\n\nfunc formatLines(prefix string, lines ...string) (string, int) {\n\tdefer func() { fmt.Println(\"formatLines finished\") }()\n\n\tvar builder strings.Builder\n\tfor _, line := range lines {\n\t\tbuilder.WriteString(prefix)\n\t\tbuilder.WriteString(line)\n\t\tbuilder.WriteString(\"\\n\")\n\t}\n\treturn builder.String(), len(lines)\n}\n\nfunc main() {\n\toutput, count := formatLines(\"> \", \"First\", \"Second\", \"Third\")\n\tfmt.Print(output)\n\tfmt.Println(\"Lines formatted:\", count)\n}\n<\/code><\/pre>\n<p>Compare this to organizing code in <a href=\"https:\/\/www.kindsonthegenius.com\/python\/\">Python<\/a> with <code>def<\/code> \u2014 Go requires explicit types, supports multiple returns natively, and uses <code>defer<\/code> instead of <code>try\/finally<\/code> for cleanup. Both approaches encourage small, focused functions; Go&#8217;s type system catches mismatches at compile time.<\/p>\n<h4 id=\"reference\">7. Quick Reference<\/h4>\n<table>\n<thead>\n<tr>\n<th>Feature<\/th>\n<th>Syntax<\/th>\n<th>Typical Use<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Basic function<\/td>\n<td><code>func name(p T) R<\/code><\/td>\n<td>Reusable logic with typed input\/output<\/td>\n<\/tr>\n<tr>\n<td>Multiple returns<\/td>\n<td><code>func f() (T, error)<\/code><\/td>\n<td>Result plus error checking<\/td>\n<\/tr>\n<tr>\n<td>Named returns<\/td>\n<td><code>func f() (x int, err error)<\/code><\/td>\n<td>Short functions, naked return<\/td>\n<\/tr>\n<tr>\n<td>Variadic<\/td>\n<td><code>func f(args ...T)<\/code><\/td>\n<td>Variable argument count<\/td>\n<\/tr>\n<tr>\n<td>defer<\/td>\n<td><code>defer cleanup()<\/code><\/td>\n<td>Run on function exit<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Exported functions (capitalized names) form the public API of a package. Keep functions small enough to read in one screen \u2014 if a function grows past roughly forty lines, consider splitting it.<\/p>\n<h4 id=\"next\">8. Next Steps<\/h4>\n<p>You now understand the <strong>go functions tutorial<\/strong> core: declarations, multiple return values, named returns, variadic parameters, and <code>defer<\/code>. Upcoming lessons cover arrays, slices, maps, and structs \u2014 data structures that functions operate on at scale.<\/p>\n<p>Continue the series: <a href=\"https:\/\/www.kindsonthegenius.com\/go\/05-go-control-flow\/\">Lesson 5 \u2013 Control Flow<\/a> \u00b7 <a href=\"https:\/\/www.kindsonthegenius.com\/go\/04-go-variables-and-types\/\">Lesson 4 \u2013 Variables and Types<\/a><\/p>\n<p>Browse all tutorials: <a href=\"https:\/\/www.kindsonthegenius.com\/content-directory\/\">Content Directory<\/a> \u00b7 <a href=\"https:\/\/www.kindsonthegenius.com\/go\/\">Go Programming Hub<\/a><\/p>\n<h4 id=\"faq\">Frequently Asked Questions<\/h4>\n<p><strong>Can Go functions return multiple values?<\/strong><br \/>\nYes. List return types in parentheses: <code>func f() (int, error)<\/code>. Callers receive all values: <code>result, err := f()<\/code>.<\/p>\n<p><strong>What does defer do in Go?<\/strong><br \/>\n<code>defer<\/code> postpones a function call until the surrounding function returns. Deferred calls run in reverse order (LIFO). It is ideal for cleanup such as closing files or releasing locks.<\/p>\n<p><strong>What is a variadic function?<\/strong><br \/>\nA variadic function accepts any number of arguments of one type using <code>...<\/code> syntax, for example <code>func sum(nums ...int)<\/code>. Inside the function, the parameter is a slice.<\/p>\n<p><!-- ktg-lesson-nav --><\/p>\n<nav class=\"ktg-lesson-nav\" aria-label=\"Lesson navigation\">\n<p><strong>Previous:<\/strong> <a href=\"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/05-go-control-flow\/\">Lesson 5: Go Control Flow \u2013 if, else, switch, and for Loops<\/a><\/p>\n<p><strong>Next:<\/strong> <a href=\"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/07-go-arrays-slices-maps\/\">Lesson 7: Go Arrays, Slices, and Maps \u2013 Working with Collections<\/a><\/p>\n<\/nav>\n<p><!-- ktg-alkademy-cta --><\/p>\n<div class=\"ktg-alkademy-cta\" style=\"margin:2em 0;padding:1.25em;border-left:4px solid #2563eb;background:#f8fafc;\">\n<p><strong>Want live Go or backend classes?<\/strong> Join <a href=\"https:\/\/www.alkademy.com\/courses\" target=\"_blank\" rel=\"noopener noreferrer\">Alkademy<\/a> for instructor-led programming courses with hands-on projects.<\/p>\n<\/div>\n<p><!-- ktg-faq-schema --><br \/>\n<script type=\"application\/ld+json\">{\"@context\":\"https:\/\/schema.org\",\"@type\":\"FAQPage\",\"mainEntity\":[{\"@type\":\"Question\",\"name\":\"Can Go functions return multiple values?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Yes. Returning (result, error) is the standard Go pattern for operations that can fail.\"}},{\"@type\":\"Question\",\"name\":\"What are named return values in Go?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"You can name return variables in the signature. They act as variables in the function body.\"}},{\"@type\":\"Question\",\"name\":\"What is a variadic function in Go?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"A function that accepts zero or more arguments of the same type using ...T syntax.\"}}]}<\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Updated July 2, 2026. Refreshed for Go 1.22+ and current SEO best practices. In this lesson we cover the Go functions tutorial fundamentals \u2014 how &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[23],"tags":[],"class_list":["post-165","post","type-post","status-publish","format-standard","hentry","category-go-programming"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 4.9.9 - aioseo.com -->\n\t<meta name=\"description\" content=\"Updated July 2, 2026. Refreshed for Go 1.22+ and current SEO best practices. In this lesson we cover the Go functions tutorial fundamentals \u2014 how to declare functions, return multiple values, use named return parameters, accept a variable number of arguments, and schedule cleanup with defer. If you completed Lesson 5 \u2013 Control Flow, you\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"kindsonthegenius\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/06-go-functions\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 4.9.9\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"Go Programming Tutorial - Beginner to Expert Go Programming Tutorial\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"Go Functions \u2013 Declarations, Returns, and defer - Go Programming Tutorial\" \/>\n\t\t<meta property=\"og:description\" content=\"Updated July 2, 2026. Refreshed for Go 1.22+ and current SEO best practices. In this lesson we cover the Go functions tutorial fundamentals \u2014 how to declare functions, return multiple values, use named return parameters, accept a variable number of arguments, and schedule cleanup with defer. If you completed Lesson 5 \u2013 Control Flow, you\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/06-go-functions\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2026-07-07T20:29:23+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2026-07-07T20:38:10+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Go Functions \u2013 Declarations, Returns, and defer - Go Programming Tutorial\" \/>\n\t\t<meta name=\"twitter:description\" content=\"Updated July 2, 2026. Refreshed for Go 1.22+ and current SEO best practices. In this lesson we cover the Go functions tutorial fundamentals \u2014 how to declare functions, return multiple values, use named return parameters, accept a variable number of arguments, and schedule cleanup with defer. If you completed Lesson 5 \u2013 Control Flow, you\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"BlogPosting\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/2026\\\/07\\\/07\\\/06-go-functions\\\/#blogposting\",\"name\":\"Go Functions \\u2013 Declarations, Returns, and defer - Go Programming Tutorial\",\"headline\":\"Go Functions \\u2013 Declarations, Returns, and defer\",\"author\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/author\\\/kindsonthegenius-3\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/#organization\"},\"datePublished\":\"2026-07-07T20:29:23+00:00\",\"dateModified\":\"2026-07-07T20:38:10+00:00\",\"inLanguage\":\"en-US\",\"commentCount\":1,\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/2026\\\/07\\\/07\\\/06-go-functions\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/2026\\\/07\\\/07\\\/06-go-functions\\\/#webpage\"},\"articleSection\":\"Go Programming\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/2026\\\/07\\\/07\\\/06-go-functions\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/category\\\/go-programming\\\/#listItem\",\"name\":\"Go Programming\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/category\\\/go-programming\\\/#listItem\",\"position\":2,\"name\":\"Go Programming\",\"item\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/category\\\/go-programming\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/2026\\\/07\\\/07\\\/06-go-functions\\\/#listItem\",\"name\":\"Go Functions \\u2013 Declarations, Returns, and defer\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/2026\\\/07\\\/07\\\/06-go-functions\\\/#listItem\",\"position\":3,\"name\":\"Go Functions \\u2013 Declarations, Returns, and defer\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/category\\\/go-programming\\\/#listItem\",\"name\":\"Go Programming\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/#organization\",\"name\":\"Go Programming Tutorial\",\"description\":\"Beginner to Expert Go Programming Tutorial\",\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/author\\\/kindsonthegenius-3\\\/#author\",\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/author\\\/kindsonthegenius-3\\\/\",\"name\":\"kindsonthegenius\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/2026\\\/07\\\/07\\\/06-go-functions\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"kindsonthegenius\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/2026\\\/07\\\/07\\\/06-go-functions\\\/#webpage\",\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/2026\\\/07\\\/07\\\/06-go-functions\\\/\",\"name\":\"Go Functions \\u2013 Declarations, Returns, and defer - Go Programming Tutorial\",\"description\":\"Updated July 2, 2026. Refreshed for Go 1.22+ and current SEO best practices. In this lesson we cover the Go functions tutorial fundamentals \\u2014 how to declare functions, return multiple values, use named return parameters, accept a variable number of arguments, and schedule cleanup with defer. If you completed Lesson 5 \\u2013 Control Flow, you\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/2026\\\/07\\\/07\\\/06-go-functions\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/author\\\/kindsonthegenius-3\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/author\\\/kindsonthegenius-3\\\/#author\"},\"datePublished\":\"2026-07-07T20:29:23+00:00\",\"dateModified\":\"2026-07-07T20:38:10+00:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/#website\",\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/\",\"name\":\"Go Programming Tutorial\",\"description\":\"Beginner to Expert Go Programming Tutorial\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Go Functions \u2013 Declarations, Returns, and defer - Go Programming Tutorial","description":"Updated July 2, 2026. Refreshed for Go 1.22+ and current SEO best practices. In this lesson we cover the Go functions tutorial fundamentals \u2014 how to declare functions, return multiple values, use named return parameters, accept a variable number of arguments, and schedule cleanup with defer. If you completed Lesson 5 \u2013 Control Flow, you","canonical_url":"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/06-go-functions\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"BlogPosting","@id":"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/06-go-functions\/#blogposting","name":"Go Functions \u2013 Declarations, Returns, and defer - Go Programming Tutorial","headline":"Go Functions \u2013 Declarations, Returns, and defer","author":{"@id":"https:\/\/kindsonthegenius.com\/go\/author\/kindsonthegenius-3\/#author"},"publisher":{"@id":"https:\/\/kindsonthegenius.com\/go\/#organization"},"datePublished":"2026-07-07T20:29:23+00:00","dateModified":"2026-07-07T20:38:10+00:00","inLanguage":"en-US","commentCount":1,"mainEntityOfPage":{"@id":"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/06-go-functions\/#webpage"},"isPartOf":{"@id":"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/06-go-functions\/#webpage"},"articleSection":"Go Programming"},{"@type":"BreadcrumbList","@id":"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/06-go-functions\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/kindsonthegenius.com\/go#listItem","position":1,"name":"Home","item":"https:\/\/kindsonthegenius.com\/go","nextItem":{"@type":"ListItem","@id":"https:\/\/kindsonthegenius.com\/go\/category\/go-programming\/#listItem","name":"Go Programming"}},{"@type":"ListItem","@id":"https:\/\/kindsonthegenius.com\/go\/category\/go-programming\/#listItem","position":2,"name":"Go Programming","item":"https:\/\/kindsonthegenius.com\/go\/category\/go-programming\/","nextItem":{"@type":"ListItem","@id":"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/06-go-functions\/#listItem","name":"Go Functions \u2013 Declarations, Returns, and defer"},"previousItem":{"@type":"ListItem","@id":"https:\/\/kindsonthegenius.com\/go#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/06-go-functions\/#listItem","position":3,"name":"Go Functions \u2013 Declarations, Returns, and defer","previousItem":{"@type":"ListItem","@id":"https:\/\/kindsonthegenius.com\/go\/category\/go-programming\/#listItem","name":"Go Programming"}}]},{"@type":"Organization","@id":"https:\/\/kindsonthegenius.com\/go\/#organization","name":"Go Programming Tutorial","description":"Beginner to Expert Go Programming Tutorial","url":"https:\/\/kindsonthegenius.com\/go\/"},{"@type":"Person","@id":"https:\/\/kindsonthegenius.com\/go\/author\/kindsonthegenius-3\/#author","url":"https:\/\/kindsonthegenius.com\/go\/author\/kindsonthegenius-3\/","name":"kindsonthegenius","image":{"@type":"ImageObject","@id":"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/06-go-functions\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g","width":96,"height":96,"caption":"kindsonthegenius"}},{"@type":"WebPage","@id":"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/06-go-functions\/#webpage","url":"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/06-go-functions\/","name":"Go Functions \u2013 Declarations, Returns, and defer - Go Programming Tutorial","description":"Updated July 2, 2026. Refreshed for Go 1.22+ and current SEO best practices. In this lesson we cover the Go functions tutorial fundamentals \u2014 how to declare functions, return multiple values, use named return parameters, accept a variable number of arguments, and schedule cleanup with defer. If you completed Lesson 5 \u2013 Control Flow, you","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/kindsonthegenius.com\/go\/#website"},"breadcrumb":{"@id":"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/06-go-functions\/#breadcrumblist"},"author":{"@id":"https:\/\/kindsonthegenius.com\/go\/author\/kindsonthegenius-3\/#author"},"creator":{"@id":"https:\/\/kindsonthegenius.com\/go\/author\/kindsonthegenius-3\/#author"},"datePublished":"2026-07-07T20:29:23+00:00","dateModified":"2026-07-07T20:38:10+00:00"},{"@type":"WebSite","@id":"https:\/\/kindsonthegenius.com\/go\/#website","url":"https:\/\/kindsonthegenius.com\/go\/","name":"Go Programming Tutorial","description":"Beginner to Expert Go Programming Tutorial","inLanguage":"en-US","publisher":{"@id":"https:\/\/kindsonthegenius.com\/go\/#organization"}}]},"og:locale":"en_US","og:site_name":"Go Programming Tutorial - Beginner to Expert Go Programming Tutorial","og:type":"article","og:title":"Go Functions \u2013 Declarations, Returns, and defer - Go Programming Tutorial","og:description":"Updated July 2, 2026. Refreshed for Go 1.22+ and current SEO best practices. In this lesson we cover the Go functions tutorial fundamentals \u2014 how to declare functions, return multiple values, use named return parameters, accept a variable number of arguments, and schedule cleanup with defer. If you completed Lesson 5 \u2013 Control Flow, you","og:url":"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/06-go-functions\/","article:published_time":"2026-07-07T20:29:23+00:00","article:modified_time":"2026-07-07T20:38:10+00:00","twitter:card":"summary_large_image","twitter:title":"Go Functions \u2013 Declarations, Returns, and defer - Go Programming Tutorial","twitter:description":"Updated July 2, 2026. Refreshed for Go 1.22+ and current SEO best practices. In this lesson we cover the Go functions tutorial fundamentals \u2014 how to declare functions, return multiple values, use named return parameters, accept a variable number of arguments, and schedule cleanup with defer. If you completed Lesson 5 \u2013 Control Flow, you"},"aioseo_meta_data":[],"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/kindsonthegenius.com\/go\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/kindsonthegenius.com\/go\/category\/go-programming\/\" title=\"Go Programming\">Go Programming<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tGo Functions \u2013 Declarations, Returns, and defer\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/kindsonthegenius.com\/go"},{"label":"Go Programming","link":"https:\/\/kindsonthegenius.com\/go\/category\/go-programming\/"},{"label":"Go Functions \u2013 Declarations, Returns, and defer","link":"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/06-go-functions\/"}],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Go Functions \u2013 Declarations, Returns, and defer | Go Programming Tutorial<\/title>\n<meta name=\"description\" content=\"Learn Go functions \u2014 parameters, multiple return values, named returns, and variadic functions. Lesson 6 in the free Go tutorial with code examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/06-go-functions\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Go Functions \u2013 Declarations, Returns, and defer | Go Programming Tutorial\" \/>\n<meta property=\"og:description\" content=\"Learn Go functions \u2014 parameters, multiple return values, named returns, and variadic functions. Lesson 6 in the free Go tutorial with code examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/06-go-functions\/\" \/>\n<meta property=\"og:site_name\" content=\"Go Programming Tutorial\" \/>\n<meta property=\"article:published_time\" content=\"2026-07-07T20:29:23+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-07-07T20:38:10+00:00\" \/>\n<meta name=\"author\" content=\"kindsonthegenius\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"kindsonthegenius\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/2026\\\/07\\\/07\\\/06-go-functions\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/2026\\\/07\\\/07\\\/06-go-functions\\\/\"},\"author\":{\"name\":\"kindsonthegenius\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/#\\\/schema\\\/person\\\/1b5971e6d80495780d9857f155fd1b21\"},\"headline\":\"Go Functions \u2013 Declarations, Returns, and defer\",\"datePublished\":\"2026-07-07T20:29:23+00:00\",\"dateModified\":\"2026-07-07T20:38:10+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/2026\\\/07\\\/07\\\/06-go-functions\\\/\"},\"wordCount\":732,\"commentCount\":1,\"articleSection\":[\"Go Programming\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/2026\\\/07\\\/07\\\/06-go-functions\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/2026\\\/07\\\/07\\\/06-go-functions\\\/\",\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/2026\\\/07\\\/07\\\/06-go-functions\\\/\",\"name\":\"Go Functions \u2013 Declarations, Returns, and defer | Go Programming Tutorial\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/#website\"},\"datePublished\":\"2026-07-07T20:29:23+00:00\",\"dateModified\":\"2026-07-07T20:38:10+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/#\\\/schema\\\/person\\\/1b5971e6d80495780d9857f155fd1b21\"},\"description\":\"Learn Go functions \u2014 parameters, multiple return values, named returns, and variadic functions. Lesson 6 in the free Go tutorial with code examples.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/2026\\\/07\\\/07\\\/06-go-functions\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/2026\\\/07\\\/07\\\/06-go-functions\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/2026\\\/07\\\/07\\\/06-go-functions\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Go Functions \u2013 Declarations, Returns, and defer\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/#website\",\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/\",\"name\":\"Go Programming Tutorial\",\"description\":\"Beginner to Expert Go Programming Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/#\\\/schema\\\/person\\\/1b5971e6d80495780d9857f155fd1b21\",\"name\":\"kindsonthegenius\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g\",\"caption\":\"kindsonthegenius\"},\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/author\\\/kindsonthegenius-3\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Go Functions \u2013 Declarations, Returns, and defer | Go Programming Tutorial","description":"Learn Go functions \u2014 parameters, multiple return values, named returns, and variadic functions. Lesson 6 in the free Go tutorial with code examples.","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:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/06-go-functions\/","og_locale":"en_US","og_type":"article","og_title":"Go Functions \u2013 Declarations, Returns, and defer | Go Programming Tutorial","og_description":"Learn Go functions \u2014 parameters, multiple return values, named returns, and variadic functions. Lesson 6 in the free Go tutorial with code examples.","og_url":"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/06-go-functions\/","og_site_name":"Go Programming Tutorial","article_published_time":"2026-07-07T20:29:23+00:00","article_modified_time":"2026-07-07T20:38:10+00:00","author":"kindsonthegenius","twitter_card":"summary_large_image","twitter_misc":{"Written by":"kindsonthegenius","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/06-go-functions\/#article","isPartOf":{"@id":"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/06-go-functions\/"},"author":{"name":"kindsonthegenius","@id":"https:\/\/kindsonthegenius.com\/go\/#\/schema\/person\/1b5971e6d80495780d9857f155fd1b21"},"headline":"Go Functions \u2013 Declarations, Returns, and defer","datePublished":"2026-07-07T20:29:23+00:00","dateModified":"2026-07-07T20:38:10+00:00","mainEntityOfPage":{"@id":"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/06-go-functions\/"},"wordCount":732,"commentCount":1,"articleSection":["Go Programming"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/06-go-functions\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/06-go-functions\/","url":"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/06-go-functions\/","name":"Go Functions \u2013 Declarations, Returns, and defer | Go Programming Tutorial","isPartOf":{"@id":"https:\/\/kindsonthegenius.com\/go\/#website"},"datePublished":"2026-07-07T20:29:23+00:00","dateModified":"2026-07-07T20:38:10+00:00","author":{"@id":"https:\/\/kindsonthegenius.com\/go\/#\/schema\/person\/1b5971e6d80495780d9857f155fd1b21"},"description":"Learn Go functions \u2014 parameters, multiple return values, named returns, and variadic functions. Lesson 6 in the free Go tutorial with code examples.","breadcrumb":{"@id":"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/06-go-functions\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/06-go-functions\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/06-go-functions\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/kindsonthegenius.com\/go\/"},{"@type":"ListItem","position":2,"name":"Go Functions \u2013 Declarations, Returns, and defer"}]},{"@type":"WebSite","@id":"https:\/\/kindsonthegenius.com\/go\/#website","url":"https:\/\/kindsonthegenius.com\/go\/","name":"Go Programming Tutorial","description":"Beginner to Expert Go Programming Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/kindsonthegenius.com\/go\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/kindsonthegenius.com\/go\/#\/schema\/person\/1b5971e6d80495780d9857f155fd1b21","name":"kindsonthegenius","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g","caption":"kindsonthegenius"},"url":"https:\/\/kindsonthegenius.com\/go\/author\/kindsonthegenius-3\/"}]}},"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/go\/wp-json\/wp\/v2\/posts\/165","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/go\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/go\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/go\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/go\/wp-json\/wp\/v2\/comments?post=165"}],"version-history":[{"count":3,"href":"https:\/\/kindsonthegenius.com\/go\/wp-json\/wp\/v2\/posts\/165\/revisions"}],"predecessor-version":[{"id":191,"href":"https:\/\/kindsonthegenius.com\/go\/wp-json\/wp\/v2\/posts\/165\/revisions\/191"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/go\/wp-json\/wp\/v2\/media?parent=165"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/go\/wp-json\/wp\/v2\/categories?post=165"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/go\/wp-json\/wp\/v2\/tags?post=165"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}