atomic.AddInt64() Function in Golang With Examples
Last Updated :
31 Jan, 2022
In Go language,
atomic packages supply lower-level atomic memory that is helpful is implementing synchronization algorithms. The
AddInt64() function in Go language is used to automatically add
delta to the
*addr. This function is defined under the atomic package. Here, you need to import "sync/atomic" package in order to use these functions.
Syntax:
func AddInt64(addr *int64, delta int64) (new int64)
Here,
addr indicates address and delta indicates a small number of bits greater than zero.
Note: (*int64) is the pointer to a int64 value. Moreover, int64 contains the set of all signed 64-bit integers from -9223372036854775808 to 9223372036854775807.
Return Value: It adds addr and delta automatically and returns a new value.
Example 1:
C
// Golang program to illustrate the usage of
// AddInt64 function
// Including main package
package main
// importing fmt and sync/atomic
import (
"fmt"
"sync/atomic"
)
// Main function
func main() {
// Assigning values
// to the int64
var (
s int64 = 67656
t int64 = 90
u int64 = 922337203685477580
v int64 = -9223372036854775807
)
// Assigning constant
// values to int64
const (
w int64 = 5
x int64 = 8
)
// Calling AddInt64 method
// with its parameters
output_1 := atomic.AddInt64(&s, w)
output_2 := atomic.AddInt64(&t, x-w)
output_3 := atomic.AddInt64(&u, x-6)
output_4 := atomic.AddInt64(&v, -x)
// Displays the output after adding
// addr and delta automatically
fmt.Println(output_1)
fmt.Println(output_2)
fmt.Println(output_3)
fmt.Println(output_4)
}
Output:
67661
93
922337203685477582
9223372036854775801
Example 2:
C
// Golang Program to illustrate the usage of
// AddInt64 function
// Including main package
package main
// importing fmt and sync/atomic
import (
"fmt"
"sync/atomic"
)
// Defining addr of type int64
type addr int64
// function that adds addr and delta
func (s *addr) adds() int64 {
// Calling AddInt64() function
// with its parameter
return atomic.AddInt64((*int64)(s), 9)
}
// Main function
func main() {
// Defining s
var s addr
// For loop to increment
// the value of s
for i := 1; i < 10000; i *= 5 {
// Displays the new value
// after adding delta and addr
fmt.Println(s.adds())
}
}
Output:
9
18
27
36
45
54
In the above example, we have defined a function
adds that returns the output returned from calling
AddInt64 method. In the main function, we have defined a "for" loop that will increment the value of 's' in each call. Here, the second parameter of the AddInt64() method is constant and only the value of the first parameter is variable. However, the output of the previous call will be the value of the first parameter of the AddInt64() method in the next call until the loop stops.
Lets see how above example works:
1st parameter = 0, 2nd parameter = 9 // returns (0 + 9 = 9)
// Now, the above output is 1st parameter in next call to AddInt64() method
1st parameter = 9, 2nd parameter = 9 // returns (9 + 9 = 18)
1st parameter = 18, 2nd parameter = 9 // returns (18 + 9 = 27) and so on.
Similar Reads
atomic.AddUint64() Function in Golang With Examples In Go language, atomic packages supply lower level atomic memory that is helpful is implementing synchronization algorithms. The AddUint64() function in Go language is used to automatically add delta to the *addr. This function is defined under the atomic package. Here, you need to import "sync/atom
3 min read
atomic.AddInt32() Function in Golang With Examples In Go language, atomic packages supply lower-level atomic memory that is helpful is implementing synchronization algorithms. The AddInt32() function in Golang is used to atomically add delta to the *addr. This function is defined under the atomic package. Here, you need to import "sync/atomic" packa
3 min read
atomic.AddUint32() Function in Golang With Examples In Go language, atomic packages supply lower-level atomic memory that is helpful is implementing synchronization algorithms. The AddUint32() function in Go language is used to automatically add delta to the *addr.This function is defined under the atomic package. Here, you need to import "sync/atomi
3 min read
atomic.AddUintptr() Function in Golang With Examples In Go language, atomic packages supply lower-level atomic memory that is helpful is implementing synchronization algorithms. The AddUintptr() function in Go language is used to automatically add delta to the *addr. This function is defined under the atomic package. Here, you need to import "sync/ato
3 min read
bits.Add64() Function in Golang with Examples Go language provides inbuilt support for bits to implement bit counting and manipulation functions for the predeclared unsigned integer types with the help of bits package. This package provides Add64() function which is used to find the sum with the carry of a, b and carry, i.e, sum = a + b + carry
2 min read
bits.Add() Function in Golang with Examples Go language provides inbuilt support for bits to implement bit counting and manipulation functions for the predeclared unsigned integer types with the help of bits package. This package provides Add() function which is used to find the sum with the carry of a, b and carry, i.e, sum = a + b + carry.
2 min read
bits.Add32() Function in Golang with Examples Go language provides inbuilt support for bits to implement bit counting and manipulation functions for the predeclared unsigned integer types with the help of bits package. This package provides Add32() function which is used to find the sum with the carry of a, b and carry, i.e, sum = a + b + carry
2 min read
bits.Div() function in Golang with Examples Go language provides inbuilt support for bits to implement bit counting and manipulation functions for the predeclared unsigned integer types with the help of bits package. This package provides Div() function which is used to find the quotient and remainder of (a, b) divided by c, i.e., q = (a, b)/
2 min read
bits.Div64() Function in Golang with Examples Go language provides inbuilt support for bits to implement bit counting and manipulation functions for the predeclared unsigned integer types with the help of bits package. This package provides Div64() function which is used to find the quotient and remainder of (a, b) divided by c, i.e., q = (a, b
2 min read
bits.Div32() Function in Golang with Examples Go language provides inbuilt support for bits to implement bit counting and manipulation functions for the predeclared unsigned integer types with the help of bits package. This package provides Div32() function which is used to find the quotient and remainder of (a, b) divided by c, i.e., q = (a, b
2 min read