在Go语言中,原子包提供lower-level原子内存,这对实现同步算法很有帮助。 Go语言的AddInt64()函数用于将增量自动添加到* addr。此函数在原子包下定义。在这里,您需要导入“sync/atomic”软件包才能使用这些函数。
用法:
func AddInt64(addr *int64, delta int64) (new int64)
在此,addr表示地址,而delta表示少量大于零的位。
注意:(* int64)是指向int64值的指针。此外,int64包含从-9223372036854775808到9223372036854775807的所有带符号的64位整数的集合。
返回值:它自动添加addr和delta并返回一个新值。
范例1:
// 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 automically
fmt.Println(output_1)
fmt.Println(output_2)
fmt.Println(output_3)
fmt.Println(output_4)
}
输出:
67661 93 922337203685477582 9223372036854775801
范例2:
// 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())
}
}
输出:
9 18 27 36 45 54
在上面的示例中,我们定义了一个add函数,该函数返回调用AddInt64方法返回的输出。在主函数中,我们定义了一个“for”循环,该循环将在每个调用中增加‘s’的值。在这里,AddInt64()方法的第二个参数是恒定的,只有第一个参数的值是可变的。但是,上一个调用的输出将是下一个调用中AddInt64()方法的第一个参数的值,直到循环停止为止。
让我们看看上面的示例如何工作:
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.
相关用法
- Golang math.Lgamma()用法及代码示例
- Golang math.Float64bits()用法及代码示例
- Golang atomic.StoreInt64()用法及代码示例
- Golang reflect.FieldByIndex()用法及代码示例
- Golang string.Contains用法及代码示例
- Golang bits.Sub()用法及代码示例
- Golang io.PipeWriter.CloseWithError()用法及代码示例
- Golang time.Round()用法及代码示例
- Golang reflect.AppendSlice()用法及代码示例
- Golang reflect.ChanOf()用法及代码示例
- Golang flag.Bool()用法及代码示例
- Golang time.Sleep()用法及代码示例
- Golang time.Time.Year()用法及代码示例
- Golang reflect.DeepEqual()用法及代码示例
- Golang reflect.Indirect()用法及代码示例
- Golang reflect.CanAddr()用法及代码示例
- Golang reflect.CanInterface()用法及代码示例
- Golang reflect.CanSet()用法及代码示例
- Golang reflect.Cap()用法及代码示例
- Golang strings.ContainsRune()用法及代码示例
注:本文由纯净天空筛选整理自nidhi1352singh大神的英文原创作品 atomic.AddInt64() Function in Golang With Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。