# Golang : Arithmetic operation with numerical slices or arrays example

Got an email from a new comer to Golang from South Africa, he or she wanted to know how to perform simple operations like arithmetic on numerical slices. There is a package - GoNum - that can do a better job for these kind of problems ... which I reckon is more efficient for mathematical calculations than using Golang's standard slices. However, there is no harm to use slices if all you wanted is to perform some simple operations like plus, minus, multiplication or division on slices or array.

Below is an example of how to use slices for arithmetic operations:

```
package main
import (
"fmt"
)
func main() {
a := []int{1, 2, 3, 4}
fmt.Println("Original : ", a)
// a * 2
for i, _ := range a {
a[i] = a[i] * 2
}
fmt.Println("a * 2 : ", a)
// a + 10
for i, _ := range a {
a[i] = a[i] + 10
}
fmt.Println("a + 10 : ", a)
b := []int{5, 6, 7, 8}
fmt.Println("b : ", b)
lengthOfA := len(a)
// will cause non-constant array bound lengthOfA error
//c := [lengthOfA]int{}
// if c size is constant, it is ok to hardcode with size of 4 (hand counted)
// c := [4]int{}
// or to make your program more robust
// figure out the slice c's length during runtime with length of a slice
c := make([]int, lengthOfA)
// a + b
// FOR a slice to plus b slice, the size or number of elements in both slices must be the same!
for i, _ := range a {
c[i] = a[i] + b[i]
}
fmt.Printf("a + b : %v + %v = %v \n", a, b, c)
d := make([]int, lengthOfA)
// a * b
for i, _ := range a {
d[i] = a[i] * b[i]
}
fmt.Printf("a * b : %v * %v = %v \n", a, b, d)
// use float64 instead of int type for division
e := make([]float64, lengthOfA)
// a / b
for i, _ := range a {
e[i] = float64(a[i]) / float64(b[i])
}
fmt.Printf("a / b : %v * %v = %v \n", a, b, e)
f := make([]int, lengthOfA)
// a - b
for i, _ := range a {
f[i] = a[i] - b[i]
}
fmt.Printf("a - b : %v * %v = %v \n", a, b, f)
}
```

Output:

Original : [1 2 3 4]

a * 2 : [2 4 6 8]

a + 10 : [12 14 16 18]

b : [5 6 7 8]

a + b : [12 14 16 18] + [5 6 7 8] = [17 20 23 26]

a * b : [12 14 16 18] * [5 6 7 8] = [60 84 112 144]

a / b : [12 14 16 18] * [5 6 7 8] = [2.4 2.3333333333333335 2.2857142857142856 2.25]

a - b : [12 14 16 18] * [5 6 7 8] = [7 8 9 10]

**References:**

https://socketloop.com/tutorials/golang-combine-slices-of-complex-numbers-and-operation-example

https://www.socketloop.com/tutorials/golang-squaring-elements-in-array

#### See also : Golang : Combine slices of complex numbers and operation example

##### By Adam Ng

IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.

#### Advertisement

#### Tutorials

+6.1k Golang : Count leading or ending zeros(any item of interest) example

+12k Golang : How to check for empty array string or string?

+4k Golang : Measure execution time for a function

+6.4k Golang : Write multiple lines or divide string into multiple lines

+10.6k Golang : How to check if a file is hidden?

+25.1k Golang : JQuery AJAX post data to server and send data back to client example

+21.1k Golang : GORM read from database example

+14.2k Golang : Determine if directory is empty with os.File.Readdir() function

+18.6k Golang : Setting up/configure AWS credentials with official aws-sdk-go

+6.1k Golang : Find network service name from given port and protocol

+20k Golang : Convert uint value to string type

+13.4k Golang : Capture stdout of a child process and act according to the result