我有一個(gè)帶有*int64字段的結(jié)構(gòu)類(lèi)型。type SomeType struct { SomeField *int64}在我的代碼中的某個(gè)時(shí)候,我想聲明一個(gè)文字(比如,當(dāng)我知道所說(shuō)的值應(yīng)該是 0,或者指向 0 時(shí),你知道我的意思)instance := SomeType{ SomeField: &0,}...除了這不起作用./main.go:xx: cannot use &0 (type *int) as type *int64 in field value所以我試試這個(gè)instance := SomeType{ SomeField: &int64(0),}...但這也不起作用./main.go:xx: cannot take the address of int64(0)我該怎么做呢?我能想出的唯一解決方案是使用占位符變量var placeholder int64placeholder = 0instance := SomeType{ SomeField: &placeholder,}顯然,我的問(wèn)題含糊不清。我正在尋找一種從字面上說(shuō)明a 的方法*int64。這可以在構(gòu)造函數(shù)中使用,或者用于說(shuō)明文字結(jié)構(gòu)值,甚至可以作為其他函數(shù)的參數(shù)。但是輔助函數(shù)或使用不同類(lèi)型不是我正在尋找的解決方案。
我如何在 Go 中做一個(gè)文字 *int64?
慕尼黑8549860
2021-10-11 18:45:50