英文:
Printing value of a *big.Int field in a Go struct
问题
我有一个需要存储在结构体中的big.Int类型的变量,但是当我尝试这样做时,它会溢出。以下是代码示例:
type NumberStore struct {
mainnumber *big.Int
}
var ledger NumberStore
// 十进制表示为33753000000000000000
var largehex string = "1D46ABEAB3FC28000"
myNumber := new(big.Int)
myNumber.SetString(largehex, 16)
ledger.mainnumber = myNumber
fmt.Println(ledger)// 输出0xc0000a64c0,但我需要它是33753000000000000000
英文:
I have a big.Int I need to store inside of a struct, but when I try to do so it overflows. Code example below
type NumberStore struct {
mainnumber *big.Int
}
var ledger NumberStore
// In decimal this is 33753000000000000000
var largehex string = "1D46ABEAB3FC28000"
myNumber := new(big.Int)
myNumber.SetString(largehex, 16)
ledger.mainnumber = myNumber
fmt.Println(ledger)// Prints 0xc0000a64c0, but I need it to be 33753000000000000000
答案1
得分: 2
由于mainnumber
是NumberStore
结构体中的指针字段,所以默认情况下打印结构体只会打印指针的值,而不是指针指向的值。
正如注释所说,如果将字段导出,那么fmt.Println
将显示底层值;但如果不需要导出,那么fmt.Println(ledger.mainnumber)
应该打印出您期望的数字。以下是您的完整代码,在最后添加了一行:
package main
import (
"fmt"
"math/big"
)
type NumberStore struct {
mainnumber *big.Int
}
func main() {
var ledger NumberStore
// In decimal this is 33753000000000000000
var largehex string = "1D46ABEAB3FC28000"
myNumber := new(big.Int)
myNumber.SetString(largehex, 16)
ledger.mainnumber = myNumber
fmt.Println(ledger)
fmt.Println(ledger.mainnumber)
}
在Playground上运行,它会打印出:
{0xc000092000}
33753000000000000000
英文:
Since mainnumber
is a pointer field in your NumberStore
struct, printing out the struct by default will just print out the value of the pointer, not the value it points to.
As the comment says, if you make your field exported then fmt.Println
will show the underlying value; but if you don't need it exported, then fmt.Println(ledger.mainnumber)
should print the number you expect. Here's your full code with one line added at the end:
package main
import (
"fmt"
"math/big"
)
type NumberStore struct {
mainnumber *big.Int
}
func main() {
var ledger NumberStore
// In decimal this is 33753000000000000000
var largehex string = "1D46ABEAB3FC28000"
myNumber := new(big.Int)
myNumber.SetString(largehex, 16)
ledger.mainnumber = myNumber
fmt.Println(ledger)
fmt.Println(ledger.mainnumber)
}
Run on the Playground, it prints:
{0xc000092000}
33753000000000000000
答案2
得分: 1
通过像这样打印 fmt.Println(ledger)
,你依赖于值 ledger
的默认格式化方式。对于结构体中的每个字段,它只会打印该值的默认表示,除非它能够访问该类型的适当自定义格式化代码。对于类型为 *big.Int
的 mainnumber
,即"指向 big.Int 的指针",它只是打印指针地址。
为了让 fmt
能够访问 *big.Int
值的自定义字符串格式化代码,你可以直接传递它:fmt.Println(ledger.mainnumber)
,或者将 mainnumber
改为一个导出字段,像这样:
type NumberStore struct {
Mainnumber *big.Int
}
如果一个未导出的结构体字段,fmt
包无法自动找到该值的格式化代码(即 .String() string
方法)。
英文:
By printing like this fmt.Println(ledger)
, you're relying on the default formatting of the value ledger
. For each field in the struct, it will only print the default representation of that value, unless it can access the appropriate custom formatting code for that type. For mainnumber
of type *big.Int
, that is "pointer to big.Int", it's simply printing the pointer address.
In order to give fmt
access to the custom string formatting code for a *big.Int
value, you either need to pass it directly: fmt.Println(ledger.mainnumber)
, or change mainnumber
to an exported field, like this:
type NumberStore struct {
Mainnumber *big.Int
}
The fmt
package cannot automatically find the value's formatting code (the .String() string
method) if it is an unexported struct field.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论