{"id":9791,"date":"2022-08-10T09:32:09","date_gmt":"2022-08-10T09:32:09","guid":{"rendered":"https:\/\/prwatech.in\/blog\/?p=9791"},"modified":"2024-04-15T09:26:42","modified_gmt":"2024-04-15T09:26:42","slug":"go-constants-go-variables-and-constants","status":"publish","type":"post","link":"https:\/\/prwatech.in\/blog\/go-lang\/go-constants-go-variables-and-constants\/","title":{"rendered":"Go Constants"},"content":{"rendered":"<h2><span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Go Variables and Constants&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:513,&quot;3&quot;:{&quot;1&quot;:0},&quot;12&quot;:0}\">Go Variables and Constants<\/span><\/h2>\n<p>&nbsp;<\/p>\n<p>In Go (also known as Golang), variables and constants are fundamental concepts used for storing and managing data within programs.<\/p>\n<p>Variables in Go represent storage locations that hold values of a specific type, such as integers, strings, or custom-defined types. They are declared using the <code>var<\/code> keyword and can be assigned values that can change during program execution. Go supports various types of variables, including basic types like int, float64, string, as well as composite types like arrays, slices, structs, and pointers.<\/p>\n<p>Constants, on the other hand, are values that are and fix at compile time and cannot be change during program execution. They are declared using the <code>const<\/code> keyword and provide a way to define named values that remain constant throughout the program&#8217;s lifecycle. Constants are typically use for defining configuration values, mathematical constants, or other immutable data.<\/p>\n<p>Understanding variables and constants in Go is essential for writing clear, maintainable, and efficient code. By using variables and constants effectively, developers can manage data efficiently and ensure the correctness and stability of their Go programs.<\/p>\n<p>A Variable in which a value <a href=\"https:\/\/go.dev\/\">cannot<\/a> be <a href=\"https:\/\/prwatech.in\/blog\/go-lang\/installation-of-go-windows\/\">change<\/a>.<\/p>\n<p>The \u2018const\u2019 keyword declares the variable as \u201cconstant\u201d. That means it is unchangeable and read-only.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<p><strong>Const CONSTNAME type\u00a0 =\u00a0 value<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">Package main\r\nImport \u201cfmt\u201d\r\n\r\nConst PI=3.14\r\n\r\nFunc main() {\r\nFmt.Println(PI)\r\n}\r\n<\/pre>\n<ol>\n<li><strong>Typed constants <\/strong><\/li>\n<\/ol>\n<p>Simple variable deceleration Go Lang program.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">package main\r\n\r\nimport \"fmt\"\r\n\r\nconst X int = 10\r\n\r\nfunc main() {\r\n    fmt.Println(X)\r\n}\r\n<\/pre>\n<p>Output :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">PS C:\\GO_Language\\constant&gt; go run const1.go\r\nValue of X : 10\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>2.<strong>Untyped constants<\/strong><\/p>\n<p>Program to illustrate untyped variables.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">package main\r\n\r\nimport \"fmt\"\r\n\r\nconst X = 11 \/\/untyped constants\/without a type\r\n\r\nfunc main() {\r\n    fmt.Println(X)\r\n}\r\n<\/pre>\n<p>Output :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">PS C:\\GO_Language\\constant&gt; go run const2.go\r\nValue of X : 11\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>3.Program to illustrate mixed Constants.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">package main\r\n\r\nimport \"fmt\"\r\n\r\nconst (\r\n    X int = 1\r\n    Y     = 3343.4\r\n    Z     = \"Prwatech\"\r\n)\r\n\r\nfunc main() {\r\n    fmt.Println(X)\r\n    fmt.Println(Y)\r\n    fmt.Println(Z)\r\n\r\n}\r\n<\/pre>\n<p>Output :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">PS C:\\GO_Language\\constant&gt; go run const3.go\r\nInteger Value : 1\r\nFloat Value   : 3343.4\r\nString Value  : Prwatech\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Go Variables and Constants &nbsp; In Go (also known as Golang), variables and constants are fundamental concepts used for storing and managing data within programs. Variables in Go represent storage locations that hold values of a specific type, such as integers, strings, or custom-defined types. They are declared using the var keyword and can be [&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":[668,667,2016,2017],"class_list":["post-9791","post","type-post","status-publish","format-standard","hentry","category-go-lang","category-golang-modules","tag-go","tag-go-lang","tag-how-to-declare-constants-in-go","tag-list-of-golang-constants-with-programming-examples"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Go Variables and Constants - Prwatech<\/title>\n<meta name=\"description\" content=\"Master Go Variables and Constants - Dive deep with our expert instructors and comprehensive curriculum, Enroll now.\" \/>\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=\"Go Variables and Constants - Prwatech\" \/>\n<meta property=\"og:description\" content=\"Master Go Variables and Constants - Dive deep with our expert instructors and comprehensive curriculum, Enroll now.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prwatech.in\/blog\/go-lang\/go-constants-go-variables-and-constants\/\" \/>\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-10T09:32:09+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-15T09:26:42+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\/go-constants-go-variables-and-constants\/\",\"url\":\"https:\/\/prwatech.in\/blog\/go-lang\/go-constants-go-variables-and-constants\/\",\"name\":\"Go Variables and Constants - Prwatech\",\"isPartOf\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#website\"},\"datePublished\":\"2022-08-10T09:32:09+00:00\",\"dateModified\":\"2024-04-15T09:26:42+00:00\",\"author\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\"},\"description\":\"Master Go Variables and Constants - Dive deep with our expert instructors and comprehensive curriculum, Enroll now.\",\"breadcrumb\":{\"@id\":\"https:\/\/prwatech.in\/blog\/go-lang\/go-constants-go-variables-and-constants\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prwatech.in\/blog\/go-lang\/go-constants-go-variables-and-constants\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prwatech.in\/blog\/go-lang\/go-constants-go-variables-and-constants\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/prwatech.in\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Go Constants\"}]},{\"@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":"Go Variables and Constants - Prwatech","description":"Master Go Variables and Constants - Dive deep with our expert instructors and comprehensive curriculum, Enroll now.","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":"Go Variables and Constants - Prwatech","og_description":"Master Go Variables and Constants - Dive deep with our expert instructors and comprehensive curriculum, Enroll now.","og_url":"https:\/\/prwatech.in\/blog\/go-lang\/go-constants-go-variables-and-constants\/","og_site_name":"Prwatech","article_publisher":"https:\/\/www.facebook.com\/prwatech.in\/","article_published_time":"2022-08-10T09:32:09+00:00","article_modified_time":"2024-04-15T09:26:42+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\/go-constants-go-variables-and-constants\/","url":"https:\/\/prwatech.in\/blog\/go-lang\/go-constants-go-variables-and-constants\/","name":"Go Variables and Constants - Prwatech","isPartOf":{"@id":"https:\/\/prwatech.in\/blog\/#website"},"datePublished":"2022-08-10T09:32:09+00:00","dateModified":"2024-04-15T09:26:42+00:00","author":{"@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3"},"description":"Master Go Variables and Constants - Dive deep with our expert instructors and comprehensive curriculum, Enroll now.","breadcrumb":{"@id":"https:\/\/prwatech.in\/blog\/go-lang\/go-constants-go-variables-and-constants\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prwatech.in\/blog\/go-lang\/go-constants-go-variables-and-constants\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/prwatech.in\/blog\/go-lang\/go-constants-go-variables-and-constants\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/prwatech.in\/blog\/"},{"@type":"ListItem","position":2,"name":"Go Constants"}]},{"@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\/9791","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=9791"}],"version-history":[{"count":6,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/9791\/revisions"}],"predecessor-version":[{"id":11519,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/9791\/revisions\/11519"}],"wp:attachment":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/media?parent=9791"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/categories?post=9791"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/tags?post=9791"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}