英文:
How to chain operations in go using big package?
问题
例如,如果我想执行 r = a * (b - c)。我会这样做:
var r, a, b, c, t big.Int
t.Sub(&b, &c)
r.Mul(&a, &t)
在包文档中,它说操作返回结果以允许链接。但由于使用的变量不是用作操作符参数,而只是用于存储结果,那么我如何链接操作呢?换句话说,我如何在只有一行代码的情况下编写我的示例,而不使用临时变量 t
?
英文:
For example, if I want to perform r = a * (b - c). I would do:
var r, a, b, c, t big.Int
t.Sub(&b, &c)
r.Mul(&a, &t)
In package documentation, it says that operations return result to allow chaining. But since the used variable isn't used as operator argument, but only to store result, how can I chain operations? In other words, how could I write my exemple using only one line of code, without temporary variable t
?
答案1
得分: 4
例如,在Go 1上,
package main
import (
"fmt"
"math/big"
)
func main() {
var r, a, b, c big.Int
a = *big.NewInt(7)
b = *big.NewInt(42)
c = *big.NewInt(24)
// r = a * (b - c)
r.Mul(&a, r.Sub(&b, &c))
fmt.Println(r.String())
}
输出:
126
英文:
For example, on Go 1,
package main
import (
"fmt"
"math/big"
)
func main() {
var r, a, b, c big.Int
a = *big.NewInt(7)
b = *big.NewInt(42)
c = *big.NewInt(24)
// r = a * (b - c)
r.Mul(&a, r.Sub(&b, &c))
fmt.Println(r.String())
}
Output:
126
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论