add numbers in go
To add numbers in Go, you can use the +
operator. Here are the steps to do it:
- Declare two integer variables, let's call them
num1
andnum2
, and assign them the values you want to add together. - Create a new variable, let's call it
sum
, and assign it the sum ofnum1
andnum2
. You can do this by using the+
operator and writingnum1 + num2
. - Print the value of
sum
using thefmt.Println()
function.
Here is an example of how the code would look like:
package main
import "fmt"
func main() {
num1 := 5
num2 := 7
sum := num1 + num2
fmt.Println(sum)
}
In this code, we declare two variables num1
and num2
and assign them the values 5 and 7 respectively. Then, we create a new variable sum
and assign it the sum of num1
and num2
using the +
operator. Finally, we use the fmt.Println()
function to print the value of sum
, which in this case would be 12.