{"id":9776,"date":"2022-08-04T09:27:17","date_gmt":"2022-08-04T09:27:17","guid":{"rendered":"https:\/\/prwatech.in\/blog\/?p=9776"},"modified":"2024-04-15T09:19:17","modified_gmt":"2024-04-15T09:19:17","slug":"variables-in-go-language-working-with-variables-in-go-lang","status":"publish","type":"post","link":"https:\/\/prwatech.in\/blog\/go-lang\/variables-in-go-language-working-with-variables-in-go-lang\/","title":{"rendered":"Variables in Go Language"},"content":{"rendered":"<h2><span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Working with Variables in Go lang&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:513,&quot;3&quot;:{&quot;1&quot;:0},&quot;12&quot;:0}\">Working with Variables in Go lang<\/span><\/h2>\n<p>&nbsp;<\/p>\n<p>In Go (or Golang), working with variables is fundamental to storing and manipulating data within programs. Variables in Go are containers that hold values of a specific type, such as integers, strings, booleans, or custom types defined by the programmer.<\/p>\n<p>Go requires variables to be declared with a specific type before they can be used. Once declared, variables can be assigned values, which can be updated or modified as needed during program execution. Go supports various primitive data types like int, float64, string, bool, as well as complex types such as structs, arrays, slices, maps, and pointers.<\/p>\n<p>Go variables have scope, which determines where they can be accessed within the program. Variables can be defined at different levels of scope, such as package-level, function-level, or block-level scope. The scope of a variable influences its visibility and lifetime.<\/p>\n<p>Understanding how to declare, initialize, and use variables effectively is crucial for writing efficient and readable Go code. By mastering variable usage in Go, developers can efficiently manage data and build robust, scalable applications that leverage the power of Go&#8217;s statically typed system and concurrency features.<\/p>\n<ul>\n<li>\n<h2><strong>Types of variable<\/strong><\/h2>\n<\/li>\n<\/ul>\n<ol>\n<li>Int<\/li>\n<li>Float32<\/li>\n<li>String<\/li>\n<li>Bool<\/li>\n<\/ol>\n<p><em>\u00a0<\/em><em>In Go language variables are <a href=\"https:\/\/prwatech.in\/blog\/go-lang\/installation-of-go-windows\/\">defined<\/a> in two <a href=\"https:\/\/go.dev\/\">different<\/a> ways<\/em><em>:<\/em><\/p>\n<ol>\n<li><strong>Using var keyword<\/strong><\/li>\n<\/ol>\n<p><strong>\u00a0<\/strong><strong>Syntax:<\/strong><\/p>\n<p>Var variable_name type =value<\/p>\n<p>&nbsp;<\/p>\n<ol start=\"2\">\n<li><strong>Using the short variable declaration<\/strong><\/li>\n<\/ol>\n<p><strong>\u00a0<\/strong><strong>Syntax:<\/strong><\/p>\n<p><strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/strong>Variable_name :=value<\/p>\n<p>&nbsp;<\/p>\n<ol>\n<li>Simple variable deceleration Go Lang program.\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">package main\r\nimport \"fmt\"\r\n\r\nfunc main() {\r\n    var a string = \"Prwatech\"\r\n    var b int = 323\r\n    var c bool = true\r\n\r\n    fmt.Println(a)\r\n    fmt.Println(b)\r\n    fmt.Println(c)\r\n}\r\n<\/pre>\n<p>Output :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">PS C:\\GO_Language\\Variable&gt; go run var1.go\r\nPrwatech\r\n323\r\ntrue\r\n<\/pre>\n<p>&nbsp;<\/li>\n<li>Program to illustrate local variable and global variable.\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">package main\r\nimport \"fmt\"\r\n\/\/global -- (Pascal Case)\r\nvar Val2 int = 50\r\n\/\/Package Level --(Camel Case)\r\nvar myValue int = 70\r\n\r\nfunc main() {\r\n    println(\"Global Variable :\", Val2)\r\n    println(\"Pakage Variable :\", myValue)\r\n\r\n    var a int \/\/variable declearation    &lt;-- Local Variable\r\n    a = 50    \/\/intilazation\r\n    fmt.Println(\"value of a :\", a)\r\n    var b int = 44 \/\/ variable declearation with intilazation\r\n    fmt.Println(\"value of b :\", b)\r\n    var c = 35\r\n    fmt.Println(\"value of c :\", c)\r\n    fmt.Println(\"Addition of a &amp; b :\", a+b)\r\n}\r\n<\/pre>\n<p>Output :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">PS C:\\GO_Language\\Variable&gt; go run variables.go\r\nGlobal Variable : 50\r\nPakage Variable : 70\r\nvalue of a : 50\r\nvalue of b : 44\r\nvalue of c : 35\r\nAddition of a &amp; b : 94\r\n<\/pre>\n<\/li>\n<li>Program to illustrate variable using function.\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">package main\r\nimport \"fmt\"\r\n\r\nvar x = 15\r\n\r\nfunc Prwa() {\r\n    var x = 6\r\n    fmt.Println(\"in Prwa :\", x)\r\n}\r\nfunc main() {\r\n    Prwa()\r\n    fmt.Println(\"in Main :\", x)\r\n\r\n}\r\n<\/pre>\n<p>Output :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">PS C:\\GO_Language\\Variable&gt; go run variables.go\r\nGlobal Variable : 50\r\nPakage Variable : 70\r\nvalue of a : 50\r\nvalue of b : 44\r\nvalue of c : 35\r\nAddition of a &amp; b : 94\r\n<\/pre>\n<p>&nbsp;<\/li>\n<li>Program to illustrate variables using a loop.\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">package main\r\nimport \"fmt\"\r\n\r\nfunc main() {\r\n    var Prwa = \"Google\"\r\n    fmt.Println(\"Normal String:\")\r\n    fmt.Println(\"%s\", Prwa)\r\n    fmt.Println(\"hex bytes \\n\")\r\n    for i := 0; i &lt; len(Prwa); i++ {\r\n        fmt.Println(\"%x\", Prwa[i], \"\\n\")\r\n    }\r\n}\r\n<\/pre>\n<p>Output :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">PS C:\\GO_Language\\Variable&gt; go run var3.go\r\nNormal String: %s  Google\r\nhex bytes \r\n\r\n %x  71 \r\n\r\n %x  111 \r\n\r\n %x  111 \r\n\r\n %x  103 \r\n\r\n %x  108 \r\n\r\n %x  101\r\n<\/pre>\n<p>&nbsp;<\/li>\n<li>Program to illustrate mixed variables.\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">package main\r\nimport \"fmt\"\r\n\r\nfunc main() {\r\n    var x, y, z = 5, 4.5, \"Prwatech\" \t\/\/Mixed variable declaration.\r\n    fmt.Println(\"The value of X :\", x)\r\n    fmt.Println(\"The value of Y:\", y)\r\n    fmt.Println(\"The value of Z :\", z)\r\n}\r\n<\/pre>\n<p>Output :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">PS C:\\GO_Language\\Variable&gt; go run var4.go\r\nThe value of X : 5\r\nThe value of Y: 4.5\r\nThe value of Z : Prwatech\r\n<\/pre>\n<p>Working with Variables in Go lang<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Working with Variables in Go lang &nbsp; In Go (or Golang), working with variables is fundamental to storing and manipulating data within programs. Variables in Go are containers that hold values of a specific type, such as integers, strings, booleans, or custom types defined by the programmer. Go requires variables to be declared with 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":[1560,1573,1572,1574,962],"class_list":["post-9776","post","type-post","status-publish","format-standard","hentry","category-go-lang","category-golang-modules","tag-bool","tag-float","tag-int","tag-string","tag-types-of-variable"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Working with Variables in Go lang - Prwatech<\/title>\n<meta name=\"description\" content=\"Master Working with Variables in Go lang - 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=\"Working with Variables in Go lang - Prwatech\" \/>\n<meta property=\"og:description\" content=\"Master Working with Variables in Go lang - Dive deep with our expert instructors and comprehensive curriculum.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prwatech.in\/blog\/go-lang\/variables-in-go-language-working-with-variables-in-go-lang\/\" \/>\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-08-04T09:27:17+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-15T09:19:17+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\/variables-in-go-language-working-with-variables-in-go-lang\/\",\"url\":\"https:\/\/prwatech.in\/blog\/go-lang\/variables-in-go-language-working-with-variables-in-go-lang\/\",\"name\":\"Working with Variables in Go lang - Prwatech\",\"isPartOf\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#website\"},\"datePublished\":\"2022-08-04T09:27:17+00:00\",\"dateModified\":\"2024-04-15T09:19:17+00:00\",\"author\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\"},\"description\":\"Master Working with Variables in Go lang - Dive deep with our expert instructors and comprehensive curriculum.\",\"breadcrumb\":{\"@id\":\"https:\/\/prwatech.in\/blog\/go-lang\/variables-in-go-language-working-with-variables-in-go-lang\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prwatech.in\/blog\/go-lang\/variables-in-go-language-working-with-variables-in-go-lang\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prwatech.in\/blog\/go-lang\/variables-in-go-language-working-with-variables-in-go-lang\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/prwatech.in\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Variables 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":"Working with Variables in Go lang - Prwatech","description":"Master Working with Variables in Go lang - 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":"Working with Variables in Go lang - Prwatech","og_description":"Master Working with Variables in Go lang - Dive deep with our expert instructors and comprehensive curriculum.","og_url":"https:\/\/prwatech.in\/blog\/go-lang\/variables-in-go-language-working-with-variables-in-go-lang\/","og_site_name":"Prwatech","article_publisher":"https:\/\/www.facebook.com\/prwatech.in\/","article_published_time":"2022-08-04T09:27:17+00:00","article_modified_time":"2024-04-15T09:19:17+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\/variables-in-go-language-working-with-variables-in-go-lang\/","url":"https:\/\/prwatech.in\/blog\/go-lang\/variables-in-go-language-working-with-variables-in-go-lang\/","name":"Working with Variables in Go lang - Prwatech","isPartOf":{"@id":"https:\/\/prwatech.in\/blog\/#website"},"datePublished":"2022-08-04T09:27:17+00:00","dateModified":"2024-04-15T09:19:17+00:00","author":{"@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3"},"description":"Master Working with Variables in Go lang - Dive deep with our expert instructors and comprehensive curriculum.","breadcrumb":{"@id":"https:\/\/prwatech.in\/blog\/go-lang\/variables-in-go-language-working-with-variables-in-go-lang\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prwatech.in\/blog\/go-lang\/variables-in-go-language-working-with-variables-in-go-lang\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/prwatech.in\/blog\/go-lang\/variables-in-go-language-working-with-variables-in-go-lang\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/prwatech.in\/blog\/"},{"@type":"ListItem","position":2,"name":"Variables 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\/9776","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=9776"}],"version-history":[{"count":7,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/9776\/revisions"}],"predecessor-version":[{"id":11518,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/9776\/revisions\/11518"}],"wp:attachment":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/media?parent=9776"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/categories?post=9776"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/tags?post=9776"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}