<\/span><\/h2>\n\n\n\nIn this program, we will simply declare and define two variables, and return the sum of two numbers.<\/p>\n\n\n\n
package main\n\nimport \"fmt\"\n\nfunc main() {\n\tvar sum = 0\n\n\tnum1 := 5\n\tnum2 := 13\n\n\tsum = num1 + num2\n\n\tfmt.Println(\"The sum of num1 and num2 is: \", sum)\n}\n<\/pre>\n\n\n\nOutput<\/strong><\/p>\n\n\n\nThe sum of num1 and num2 is: 18\n<\/pre>\n\n\n\n<\/span>How Does This Program Work?<\/strong><\/span><\/h2>\n\n\n\n\tvar sum = 0\n<\/pre>\n\n\n\nWe have declared a variable \u201csum\u201d<\/strong> which will store the addition of two numbers.<\/p>\n\n\n\n\tnum1 := 5\n\tnum2 := 13\n<\/pre>\n\n\n\nThe two integers are stored in two different variables \u201cnum1\u201d<\/strong> and \u201cnum2\u201d<\/strong>.<\/p>\n\n\n\n\tsum = num1 + num2\n<\/pre>\n\n\n\nThe two variables are added using the +<\/strong> operator and the result is stored in the \u201csum\u201d <\/strong>variable.<\/p>\n\n\n\n\tfmt.Println(\"The sum of num1 and num2 is: \", sum)\n<\/pre>\n\n\n\nThe sum of two variables is printed on the screen with the help of the Println()<\/strong> function.<\/p>\n\n\n\nThe Println()<\/strong> function in the Go Programming language is a built-in function used to print out the output to the console.<\/p>\n\n\n\n<\/span>Go Program to Add Two Numbers Using User Input<\/strong><\/span><\/h2>\n\n\n\nIn this program, we will take the input from the user with the help of Scanln()<\/strong> function.<\/p>\n\n\n\npackage main\n\nimport \"fmt\"\n\nfunc main() {\n\tvar num1, num2 int\n\n\tfmt.Print(\"Enter the first number: \")\n\tfmt.Scanln(&num1)\n\tfmt.Print(\"Enter the second number: \")\n\tfmt.Scanln(&num2)\n\n\tresult := num1 + num2\n\n\tfmt.Println(\"The sum of num1 and num2 is: \", result)\n}\n<\/pre>\n\n\n\nOutput<\/strong><\/p>\n\n\n\nEnter the first number: 15\nEnter the second number: 45\nThe sum of num1 and num2 is: 60\n<\/pre>\n\n\n\n<\/span>How Does This Program Work?<\/strong><\/span><\/h2>\n\n\n\nWe will use the same logic as that of the above program. In this program, we will only alter the user input. Here, we will not define the variables, but rather ask the user to do so.<\/p>\n\n\n\n
\tfmt.Print(\"Enter the first number: \")\n\tfmt.Scanln(&num1)\n\tfmt.Print(\"Enter the second number: \")\n\tfmt.Scanln(&num2)\n<\/pre>\n\n\n\nThe Scanln() function in the Go Programming language is used to read the standard input data.<\/p>\n\n\n\n
The Scanln() function in this program reads the input given by the user and stores the value in the \u201cnum1\u201d<\/strong> and \u201cnum2\u201d<\/strong> variables respectively.<\/p>\n\n\n\nThen, the addition is performed on these two variables and the result is printed on the screen using the Println()<\/strong> function.<\/p>\n\n\n\n<\/span>Conclusion<\/strong><\/span><\/h2>\n\n\n\nIn this post, you learned two different approaches to adding two numbers.<\/p>\n\n\n\n
First by using the standard method and second by taking user input.<\/p>\n","protected":false},"excerpt":{"rendered":"
Go Program to Add Two Numbers – In this post, we will learn how to add two numbers in the Go Programming language.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""}},"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[808],"tags":[811,809,810],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/posts\/8397"}],"collection":[{"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/comments?post=8397"}],"version-history":[{"count":5,"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/posts\/8397\/revisions"}],"predecessor-version":[{"id":8404,"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/posts\/8397\/revisions\/8404"}],"wp:attachment":[{"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/media?parent=8397"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/categories?post=8397"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/tags?post=8397"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}