{"id":9878,"date":"2022-09-05T09:14:05","date_gmt":"2022-09-05T09:14:05","guid":{"rendered":"https:\/\/prwatech.in\/blog\/?p=9878"},"modified":"2024-04-15T10:36:58","modified_gmt":"2024-04-15T10:36:58","slug":"function-in-go-language-how-to-define-and-call-a-function-in-golang","status":"publish","type":"post","link":"https:\/\/prwatech.in\/blog\/go-lang\/function-in-go-language-how-to-define-and-call-a-function-in-golang\/","title":{"rendered":"Function in Go Language"},"content":{"rendered":"<h2><span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;How to Define and Call a Function in Golang&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:513,&quot;3&quot;:{&quot;1&quot;:0},&quot;12&quot;:0}\">How to Define and Call a Function in Golang<\/span><\/h2>\n<p>In Go (Golang), functions are fundamental building blocks used to encapsulate reusable logic and perform specific tasks within a program. Functions in Go are defined using the <code>func<\/code> keyword followed by the function name, a list of parameters (if any), an optional return type, and a function body enclosed in curly braces <code>{}<\/code>.<\/p>\n<p>To define a function in Go, specify its name, parameters (if any), and return type (if applicable). Functions can take zero or more parameters, and they may return zero or multiple values. Parameters in Go functions are pass by value unless explicitly specified as pointers.<\/p>\n<p>Once defined, functions can be called or invoked by using their name followed by parentheses <code>()<\/code>, optionally passing arguments inside the parentheses if the function expects parameters. The function executes the logic defined in its body and may return one or more values based on the defined return type.<\/p>\n<p>In Go, functions can be define at the package level or within other functions (nested functions). They are first-class citizens, meaning functions can be assign to variables, pass as arguments to other functions, and return from functions, enabling powerful functional programming capabilities in Go. Understanding how to define and call functions is essential for writing modular and maintainable Go code.<\/p>\n<ol>\n<li>Call a Function\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">package main\r\nimport (\r\n\"fmt\"\r\n\r\n)\r\nfunc myMessage() {\r\n  fmt.Println(\"This is Prwatech Institute of Data Scientist  \r\n}\r\n\r\nfunc main() {\r\n  myMessage() \/\/ call the function\r\n}\r\n<\/pre>\n<p>Output :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">PS C:\\GO_Language\\Function&gt; go run func1.go\r\n \r\nThis is Prwatech Institute of Data Scientist\r\n\r\n<\/pre>\n<\/li>\n<li><strong><strong>Function with Parameter<\/strong><\/strong>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">package main\r\n\r\nimport (\r\n    \"fmt\"\r\n)\r\n\r\nfunc familyName(fname string) {\r\n    fmt.Println(\"Hello\", fname, \"Prwatech\")\r\n}\r\n\r\nfunc main() {\r\n    familyName(\"Guru\")\r\n    familyName(\"Sam\")\r\n    familyName(\"Anuj\")\r\n}\r\n<\/pre>\n<p>Output :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">PS C:\\GO_Language\\Function&gt; go run func2.go\r\nHello Guru Prwatech\r\nHello Sam Prwatech\r\nHello Anuj Prwatech\r\n<\/pre>\n<p>&nbsp;<\/li>\n<li><strong><strong>print the Fibonacci sequence using FUNCTION<\/strong><\/strong>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">package main\r\n\r\nimport (\r\n    \"fmt\"\r\n)\r\n\r\nfunc fib1(i int) int {\r\n\r\n    if i == 0 {\r\n        return 1\r\n    }\r\n    if i == 1 {\r\n        return 1\r\n    }\r\n    return fib1(i-1) + fib1(i-2)\r\n}\r\nfunc main() {\r\n    var i int\r\n    for i = 0; i &lt; 10; i++ {\r\n        fmt.Printf(\"%d \\n\", fib1(i))\r\n\r\n    }\r\n}\r\n<\/pre>\n<p>Output :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">PS C:\\GO_Language\\Function&gt; go run func3.go\r\n1 \r\n1 \r\n2 \r\n3 \r\n5 \r\n8 \r\n13 \r\n21 \r\n34 \r\n55\r\n<\/pre>\n<p>&nbsp;<\/p>\n<ul>\n<li><strong>Golang Passing Address to a Function<\/strong><\/li>\n<li><strong>Anonymous <a href=\"https:\/\/prwatech.in\/blog\/go-lang\/installation-of-go-windows\/\">Functions<\/a> in <a href=\"https:\/\/go.dev\/\">Golang<\/a><\/strong><\/li>\n<li><strong>Closures Functions in Golang<\/strong><\/li>\n<li><strong>Higher Order Functions in Golang<\/strong><\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p>How to Define and Call a Function in Golang<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to Define and Call a Function in Golang In Go (Golang), functions are fundamental building blocks used to encapsulate reusable logic and perform specific tasks within a program. Functions in Go are defined using the func keyword followed by the function name, a list of parameters (if any), an optional return type, and a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[666,1707],"tags":[1597,1598,1596,1595,1599],"class_list":["post-9878","post","type-post","status-publish","format-standard","hentry","category-go-lang","category-golang-modules","tag-anonymous-function-in-go-lang","tag-closures-in-golang","tag-fibonacci-sequence","tag-function-in-go-lang","tag-high-order-function"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to Define and Call a Function in Golang - Prwatech<\/title>\n<meta name=\"description\" content=\"Master How to Define and Call a Function in Golang - Dive deep with our expert instructors and comprehensive curriculum.\" \/>\n<meta name=\"robots\" content=\"noindex, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Define and Call a Function in Golang - Prwatech\" \/>\n<meta property=\"og:description\" content=\"Master How to Define and Call a Function in Golang - Dive deep with our expert instructors and comprehensive curriculum.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prwatech.in\/blog\/go-lang\/function-in-go-language-how-to-define-and-call-a-function-in-golang\/\" \/>\n<meta property=\"og:site_name\" content=\"Prwatech\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prwatech.in\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-05T09:14:05+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-15T10:36:58+00:00\" \/>\n<meta name=\"author\" content=\"Prwatech\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@Eduprwatech\" \/>\n<meta name=\"twitter:site\" content=\"@Eduprwatech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prwatech\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prwatech.in\/blog\/go-lang\/function-in-go-language-how-to-define-and-call-a-function-in-golang\/\",\"url\":\"https:\/\/prwatech.in\/blog\/go-lang\/function-in-go-language-how-to-define-and-call-a-function-in-golang\/\",\"name\":\"How to Define and Call a Function in Golang - Prwatech\",\"isPartOf\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#website\"},\"datePublished\":\"2022-09-05T09:14:05+00:00\",\"dateModified\":\"2024-04-15T10:36:58+00:00\",\"author\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\"},\"description\":\"Master How to Define and Call a Function in Golang - Dive deep with our expert instructors and comprehensive curriculum.\",\"breadcrumb\":{\"@id\":\"https:\/\/prwatech.in\/blog\/go-lang\/function-in-go-language-how-to-define-and-call-a-function-in-golang\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prwatech.in\/blog\/go-lang\/function-in-go-language-how-to-define-and-call-a-function-in-golang\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prwatech.in\/blog\/go-lang\/function-in-go-language-how-to-define-and-call-a-function-in-golang\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/prwatech.in\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Function in Go Language\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/prwatech.in\/blog\/#website\",\"url\":\"https:\/\/prwatech.in\/blog\/\",\"name\":\"Prwatech\",\"description\":\"Share Ideas, Start Something Good.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/prwatech.in\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\",\"name\":\"Prwatech\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g\",\"caption\":\"Prwatech\"},\"url\":\"https:\/\/prwatech.in\/blog\/author\/prwatech123\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Define and Call a Function in Golang - Prwatech","description":"Master How to Define and Call a Function in Golang - Dive deep with our expert instructors and comprehensive curriculum.","robots":{"index":"noindex","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"og_locale":"en_US","og_type":"article","og_title":"How to Define and Call a Function in Golang - Prwatech","og_description":"Master How to Define and Call a Function in Golang - Dive deep with our expert instructors and comprehensive curriculum.","og_url":"https:\/\/prwatech.in\/blog\/go-lang\/function-in-go-language-how-to-define-and-call-a-function-in-golang\/","og_site_name":"Prwatech","article_publisher":"https:\/\/www.facebook.com\/prwatech.in\/","article_published_time":"2022-09-05T09:14:05+00:00","article_modified_time":"2024-04-15T10:36:58+00:00","author":"Prwatech","twitter_card":"summary_large_image","twitter_creator":"@Eduprwatech","twitter_site":"@Eduprwatech","twitter_misc":{"Written by":"Prwatech","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/prwatech.in\/blog\/go-lang\/function-in-go-language-how-to-define-and-call-a-function-in-golang\/","url":"https:\/\/prwatech.in\/blog\/go-lang\/function-in-go-language-how-to-define-and-call-a-function-in-golang\/","name":"How to Define and Call a Function in Golang - Prwatech","isPartOf":{"@id":"https:\/\/prwatech.in\/blog\/#website"},"datePublished":"2022-09-05T09:14:05+00:00","dateModified":"2024-04-15T10:36:58+00:00","author":{"@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3"},"description":"Master How to Define and Call a Function in Golang - Dive deep with our expert instructors and comprehensive curriculum.","breadcrumb":{"@id":"https:\/\/prwatech.in\/blog\/go-lang\/function-in-go-language-how-to-define-and-call-a-function-in-golang\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prwatech.in\/blog\/go-lang\/function-in-go-language-how-to-define-and-call-a-function-in-golang\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/prwatech.in\/blog\/go-lang\/function-in-go-language-how-to-define-and-call-a-function-in-golang\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/prwatech.in\/blog\/"},{"@type":"ListItem","position":2,"name":"Function in Go Language"}]},{"@type":"WebSite","@id":"https:\/\/prwatech.in\/blog\/#website","url":"https:\/\/prwatech.in\/blog\/","name":"Prwatech","description":"Share Ideas, Start Something Good.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/prwatech.in\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3","name":"Prwatech","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g","caption":"Prwatech"},"url":"https:\/\/prwatech.in\/blog\/author\/prwatech123\/"}]}},"_links":{"self":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/9878","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/comments?post=9878"}],"version-history":[{"count":5,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/9878\/revisions"}],"predecessor-version":[{"id":11539,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/9878\/revisions\/11539"}],"wp:attachment":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/media?parent=9878"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/categories?post=9878"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/tags?post=9878"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}