在Go語言中,原子包提供較低級別的原子內存,這對實現同步算法很有幫助。 Go語言中的AddUint64()函數用於自動將增量添加到* addr。此函數在原子包下定義。在這裏,您需要導入“sync/atomic”軟件包才能使用這些函數。
用法:
func AddUint64(addr *uint64, delta uint64) (new uint64)
在此,addr表示地址,而delta表示少量大於零的位。此外,如果要從x減去帶符號的正常數值c,則可以通過AddUint64(&x ;,^uint64(c-1))來實現。而且,如果要特別減少x,則可以通過AddUint64(&x ;,^uint64(0))來完成。
注意:(* uint64)是指向uint64值的指針。此外,uint64包含從0到18446744073709551615的所有無符號64位整數的集合。
返回值:它自動添加addr和delta並返回一個新值。
範例1:
// Golang Program to illustrate the usage
// of AddUint64 function
// Including main package
package main
// importing fmt and sync/atomic
import (
"fmt"
"sync/atomic"
)
// Main function
func main() {
// Assigning variable
// values to the uint64
var (
i uint64 = 453
j uint64 = 167
k uint64 = 18446744073709551615
l uint64 = 0
z int = 15
)
// Assigning constant values to uint64
const (
x uint64 = 7
y uint64 = 12
)
// Calling AddUint64 method
// with its parameters
a_1:= atomic.AddUint64(&i, -uint64(z))
a_2:= atomic.AddUint64(&j, ^(y - 1))
a_3:= atomic.AddUint64(&k, x-1)
a_4:= atomic.AddUint64(&l, ^uint64(z-1))
// Displays the output after adding
// addr and delta automically
fmt.Println(a_1)
fmt.Println(a_2)
fmt.Println(a_3)
fmt.Println(a_4)
}
輸出:
438 155 5 18446744073709551601
範例2:
// Golang Program to illustrate the usage
// of AddUint64 function
// Including main package
package main
// importing fmt and sync/atomic
import (
"fmt"
"sync/atomic"
)
// Defining addr of type uint64
type addr uint64
// function that adds addr and delta
func (u *addr) adds() uint64 {
// Calling AddUint64()
// function with its
// parameter
return atomic.AddUint64((*uint64)(u), 325)
}
// Main function
func main() {
// Defining u
var u addr
// For loop to increment
// the value of u
for i:= 1; i < 11; i *= 3 {
// Displays the new value after
// adding delta and addr
fmt.Println(u.adds())
}
}
輸出:
325 650 975
在上麵的示例中,我們定義了一個add函數,該函數返回調用AddUint64方法返回的輸出。在主函數中,我們定義了一個“for”循環,該循環將在每個調用中增加‘u’的值。在這裏,AddUint64()方法的第二個參數是恒定的,隻有第一個參數的值是可變的。但是,上一個調用的輸出將是下一個調用中AddUint64()方法的第一個參數的值,直到循環停止。
讓我們看看上麵的示例如何工作:
1st parameter = 0, 2nd parameter = 325 // returns (0 + 325 = 325) // Now, the above output is 1st parameter in next call to AddUint64() method 1st parameter = 325, 2nd parameter = 325 // returns (325 + 325 = 650) 1st parameter = 650, 2nd parameter = 325 // returns (650 + 325 = 975)
相關用法
- Golang math.Lgamma()用法及代碼示例
- Golang math.Float64bits()用法及代碼示例
- Golang atomic.AddInt64()用法及代碼示例
- 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()用法及代碼示例
注:本文由純淨天空篩選整理自nidhi1352singh大神的英文原創作品 atomic.AddUint64() Function in Golang With Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。