原子。Golang 中 StoreInt64()函数示例
原文:https://www . geesforgeks . org/atomic-storeint 64-function-in-golang-with-examples/
在 Go 语言中,原子包提供低级原子内存,有助于实现同步算法。Go 语言中的 StoreInt64() 功能用于将 val 自动存储到 *addr 中。这个函数是在原子包下定义的。在这里,您需要导入“同步/原子”包才能使用这些功能。
语法:
func StoreInt64(addr *int64, val int64)
这里, addr 表示地址。
注意: (*int64)是指向 int64 值的指针。但是,int64 包含从-9223372036854775808 到 922337203685475807 的所有带符号 64 位整数的集合。
返回值:将 val 存储到*addr 中,需要时可以返回。
例 1:
// Program to illustrate the usage of
// StoreInt64 function in Golang
// Including main package
package main
// importing fmt and sync/atomic
import (
"fmt"
"sync/atomic"
)
// Main function
func main() {
// Defining variables for
// the address to store the val
var (
x int64
y int64
)
// Using StoreInt64 method
// with its parameters
atomic.StoreInt64(&x, 6777676777)
atomic.StoreInt64(&y, 98877)
// Displays the value stored in addr
fmt.Println(atomic.LoadInt64(&x))
fmt.Println(atomic.LoadInt64(&y))
}
输出:
6777676777
98877
这里,首先,int64 值存储在定义的地址中,然后使用上面的 LoadInt64()方法返回它们。
例 2:
// Program to illustrate the usage of
// StoreInt64 function in Golang
// Including main package
package main
// importing fmt and sync/atomic
import (
"fmt"
"sync/atomic"
)
// Main function
func main() {
// Defining variables for the
// address to store the val
var (
x int64
)
// Using StoreInt64 method
// with its parameters
atomic.StoreInt64(&x, 3654567899788)
// Loading the stored val
z := atomic.LoadInt64(&x)
// Prints true if values
// are same else false
fmt.Println(z == x)
// Prints true if addresses
// are same else false
fmt.Println(&z == &x)
}
输出:
true
false
这里,存储和加载的值是相同的,所以返回 true,但是它们的地址不相同,所以在这种情况下返回 false。
版权属于:月萌API www.moonapi.com,转载请注明出处