{"id":9837,"date":"2022-08-22T07:15:49","date_gmt":"2022-08-22T07:15:49","guid":{"rendered":"https:\/\/prwatech.in\/blog\/?p=9837"},"modified":"2024-04-15T09:50:03","modified_gmt":"2024-04-15T09:50:03","slug":"operators-in-golang-go-operators-with-examples","status":"publish","type":"post","link":"https:\/\/prwatech.in\/blog\/go-lang\/operators-in-golang-go-operators-with-examples\/","title":{"rendered":"Operators in Golang"},"content":{"rendered":"<h2><span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Go Operators (With Examples)&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:513,&quot;3&quot;:{&quot;1&quot;:0},&quot;12&quot;:0}\">Go Operators (With Examples)<\/span><\/h2>\n<p>&nbsp;<\/p>\n<p>In Go (or Golang), operators are symbols or keywords that perform specific operations on operands, which can be variables, constants, or expressions. Go supports a variety of operators categorized into different types, including arithmetic operators, comparison operators, logical operators, assignment operators, and more.<\/p>\n<p>Arithmetic operators (+, -, *, \/, %) are used for basic arithmetic operations like addition, subtraction, multiplication, division, and modulus. Comparison operators (==, !=, &lt;, &gt;, &lt;=, &gt;=) are used to compare values and produce boolean results based on the comparison.<\/p>\n<p>Logical operators (&amp;&amp;, ||, !) are use to perform logical operations like AND, OR, and NOT on boolean values. Bitwise operators (&amp;, |, ^, &lt;&lt;, &gt;&gt;, &amp;) are used to perform operations at the bit level.<\/p>\n<p>Assignment operators (=, +=, -=, *=, \/=, %=) are use to assign values to variables and perform compound assignment operations.<\/p>\n<p>Understanding and utilizing operators is essential for writing expressive and efficient Go programs. Operators allow developers to manipulate data, control program flow, and perform computations effectively. Mastery of Go operators enables developers to write concise and readable code while leveraging the language&#8217;s powerful capabilities for data manipulation and control.<\/p>\n<h4><strong>1. Arithmetic Operators <\/strong><\/h4>\n<p>List of different arithmetic <a href=\"https:\/\/prwatech.in\/blog\/go-lang\/installation-of-go-windows\/\">operators<\/a> in Go <a href=\"https:\/\/go.dev\/\">Language<\/a>:<\/p>\n<ul>\n<li><strong>Addition<\/strong><\/li>\n<li><strong>Subtraction<\/strong><\/li>\n<li><strong>Multiplication<\/strong><\/li>\n<li><strong>Division<\/strong><\/li>\n<li><strong>Modulus<\/strong><\/li>\n<\/ul>\n<p>Ex:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">package main\r\nimport \"fmt\"\r\nfunc main() {\r\n    var x, y, z int\r\n    x = 10\r\n    y = 20\r\n    z = x * y\r\nfmt.Println(\"Addition of x &amp; y :\", x + y)\r\nfmt.Println(\"Subtraction of x &amp; y :\", x - y)\r\nfmt.Println(\"Multiplication of no is \", z)\r\nfmt.Println(\"division of x &amp; y :\", x\/y)\r\n} \r\n<\/pre>\n<p>Output:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">PS C:\\GO_Language\\operators&gt; go run opr1.go\r\nAddition of x &amp; y : 30\r\nSubtraction of x &amp; y : -10\r\nMultiplication of no is  200\r\ndivision of x &amp; y : 0\r\n<\/pre>\n<h4><strong>2. Relational operators<\/strong><\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">package main\r\nimport \"fmt\"\r\n\r\nfunc main() {\r\n  \tvar a, b int\r\n    a, b: = 10, 20\r\n\r\n    fmt.Println(a == b)\r\n    fmt.Println(a != b)\r\n    fmt.Println(a &lt; b)\r\n    fmt.Println(a &gt; b)\r\n    fmt.Println(a &gt;= b)\r\n    fmt.Println(a &lt;= b)\r\n}\r\n<\/pre>\n<p>Output :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">PS C:\\GO_Language\\operators&gt; go run opr2.go\r\nfalse\r\ntrue\r\ntrue\r\nfalse\r\nfalse\r\ntrue\r\n<\/pre>\n<h4><strong>3. Logical Operators<\/strong><\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">package main\r\nimport \"fmt\"\r\nfunc main() {\r\n    var p int = 23\r\n    var q int = 60\r\n    if(p!=q &amp;&amp; p&lt;=q){\r\n        fmt.Println(\"True\")\r\n    }\r\n    if(p!=q || p&lt;=q){\r\n        fmt.Println(\"True\")\r\n    }\r\n    if(!(p==q)){\r\n        fmt.Println(\"True\")\r\n    }\r\n}\r\n<\/pre>\n<p>Output :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">PS C:\\GO_Language\\operators&gt; go run opr3.go\r\nTrue\r\nTrue\r\nTrue\r\n<\/pre>\n<h4>4.<strong>Bitwise Operators:<\/strong><\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">package main\r\nimport \"fmt\"\r\nfunc main() {\r\n    x: = 10\r\n    y: = 12\r\n    var z int\r\n    z = x &amp; y\r\n    fmt.Println(\"x &amp; y :\", x, y)\r\n    z = x | y\r\n    fmt.Println(\" x | y :\", z)\r\n    z = x ^ y\r\n    fmt.Println(\"x ^ y\t:\", z)\r\n    z = x &lt;&lt; y\r\n    fmt.Println(\"x &lt;&lt; y\t:\", z)\r\n    z = x &gt;&gt; y\r\n    fmt.Println(\"x &gt;&gt; y\t:\", z)\r\n}\r\n<\/pre>\n<p>Output :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">PS C:\\GO_Language\\operators&gt; go run opr4.go\r\nx &amp; y : 10 12\r\n x | y : 14\r\nx ^ y   : 6\r\nx &lt;&lt; y  : 40960\r\nx &gt;&gt; y  : 0\r\n<\/pre>\n<h4><strong>5. Assignment Operators:<\/strong><\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">package main\r\nimport \"fmt\"\r\nfunc main() {\r\n   var p int = 45\r\n    var q int = 50\r\n\r\n   \/\/ \u201c=\u201d (Simple Assignment)\r\n   p = q\r\n   fmt.Println(p)\r\n\r\n   \/\/ \u201c+=\u201d (Add Assignment)\r\n    p += q\r\n   fmt.Println(p)\r\n\r\n   \/\/ \u201c-=\u201d (Subtract Assignment)\r\n   p-=q\r\n   fmt.Println(p)\r\n\r\n   \/\/ \u201c*=\u201d (Multiply Assignment)\r\n   p*= q\r\n   fmt.Println(p)\r\n\r\n   \/\/ \u201c\/=\u201d (Division Assignment)\r\n    p \/= q\r\n   fmt.Println(p)\r\n\r\n    \/\/ \u201c%=\u201d (Modulus Assignment)\r\n    p %= q\r\n   fmt.Println(p)\r\n\r\n}\r\n<\/pre>\n<p>Output :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">PS C:\\GO_Language\\operators&gt; go run opr5.go\r\n50\r\n100\r\n50\r\n2500\r\n50\r\n0\r\n<\/pre>\n<h4>6.<strong>Misc. Operators:<\/strong><\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">package main\r\n\r\nimport \"fmt\"\r\n\r\nfunc main() {\r\n    a := 4\r\n\r\n    \/\/ Using address of operator(&amp;) and\r\n    \/\/ pointer indirection(*) operator\r\n    b := &amp;a\r\n    fmt.Println(*b)\r\n    *b = 7\r\n    fmt.Println(a)\r\n}\r\n<\/pre>\n<p>Output :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">PS C:\\GO_Language\\operators&gt; go run opr6.go\r\n4\r\n7\r\n<\/pre>\n<p>Go Operators (With Examples)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Go Operators (With Examples) &nbsp; In Go (or Golang), operators are symbols or keywords that perform specific operations on operands, which can be variables, constants, or expressions. Go supports a variety of operators categorized into different types, including arithmetic operators, comparison operators, logical operators, assignment operators, and more. Arithmetic operators (+, -, *, \/, %) [&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":[1566,1565,1564,1567,2027],"class_list":["post-9837","post","type-post","status-publish","format-standard","hentry","category-go-lang","category-golang-modules","tag-arithmetic-operator","tag-assignment-operators","tag-operators","tag-relational-operator","tag-the-go-programming-language-specification"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Go Operators (With Examples) - Prwatech<\/title>\n<meta name=\"description\" content=\"Master Go Operators (With Examples) - Dive deep with our expert instructors and comprehensive curriculum, Enroll.\" \/>\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 Operators (With Examples) - Prwatech\" \/>\n<meta property=\"og:description\" content=\"Master Go Operators (With Examples) - Dive deep with our expert instructors and comprehensive curriculum, Enroll.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prwatech.in\/blog\/go-lang\/operators-in-golang-go-operators-with-examples\/\" \/>\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-22T07:15:49+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-15T09:50:03+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=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prwatech.in\/blog\/go-lang\/operators-in-golang-go-operators-with-examples\/\",\"url\":\"https:\/\/prwatech.in\/blog\/go-lang\/operators-in-golang-go-operators-with-examples\/\",\"name\":\"Go Operators (With Examples) - Prwatech\",\"isPartOf\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#website\"},\"datePublished\":\"2022-08-22T07:15:49+00:00\",\"dateModified\":\"2024-04-15T09:50:03+00:00\",\"author\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\"},\"description\":\"Master Go Operators (With Examples) - Dive deep with our expert instructors and comprehensive curriculum, Enroll.\",\"breadcrumb\":{\"@id\":\"https:\/\/prwatech.in\/blog\/go-lang\/operators-in-golang-go-operators-with-examples\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prwatech.in\/blog\/go-lang\/operators-in-golang-go-operators-with-examples\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prwatech.in\/blog\/go-lang\/operators-in-golang-go-operators-with-examples\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/prwatech.in\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Operators in Golang\"}]},{\"@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 Operators (With Examples) - Prwatech","description":"Master Go Operators (With Examples) - Dive deep with our expert instructors and comprehensive curriculum, Enroll.","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 Operators (With Examples) - Prwatech","og_description":"Master Go Operators (With Examples) - Dive deep with our expert instructors and comprehensive curriculum, Enroll.","og_url":"https:\/\/prwatech.in\/blog\/go-lang\/operators-in-golang-go-operators-with-examples\/","og_site_name":"Prwatech","article_publisher":"https:\/\/www.facebook.com\/prwatech.in\/","article_published_time":"2022-08-22T07:15:49+00:00","article_modified_time":"2024-04-15T09:50:03+00:00","author":"Prwatech","twitter_card":"summary_large_image","twitter_creator":"@Eduprwatech","twitter_site":"@Eduprwatech","twitter_misc":{"Written by":"Prwatech","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/prwatech.in\/blog\/go-lang\/operators-in-golang-go-operators-with-examples\/","url":"https:\/\/prwatech.in\/blog\/go-lang\/operators-in-golang-go-operators-with-examples\/","name":"Go Operators (With Examples) - Prwatech","isPartOf":{"@id":"https:\/\/prwatech.in\/blog\/#website"},"datePublished":"2022-08-22T07:15:49+00:00","dateModified":"2024-04-15T09:50:03+00:00","author":{"@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3"},"description":"Master Go Operators (With Examples) - Dive deep with our expert instructors and comprehensive curriculum, Enroll.","breadcrumb":{"@id":"https:\/\/prwatech.in\/blog\/go-lang\/operators-in-golang-go-operators-with-examples\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prwatech.in\/blog\/go-lang\/operators-in-golang-go-operators-with-examples\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/prwatech.in\/blog\/go-lang\/operators-in-golang-go-operators-with-examples\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/prwatech.in\/blog\/"},{"@type":"ListItem","position":2,"name":"Operators in Golang"}]},{"@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\/9837","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=9837"}],"version-history":[{"count":5,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/9837\/revisions"}],"predecessor-version":[{"id":11526,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/9837\/revisions\/11526"}],"wp:attachment":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/media?parent=9837"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/categories?post=9837"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/tags?post=9837"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}