英文:
Set int64 value to *int64 pointer
问题
我需要将结构体映射到创建JSON结构。JSON中的collector_id属性应该能够接受null值或int值。
我有以下代码:
type purchaseInfo struct {
CollectorID *int64 `json:"collector_id"`
}
func mapPurchaseInfo(collectorID int64) purchaseInfo {
var collectorIDToSend *int64
if collectorID < 0 {
collectorIDToSend = nil
} else {
collectorIDToSend = &collectorID
}
return purchaseInfo{
CollectorID: collectorIDToSend,
}
}
这段代码无法编译,collectorID无法赋值给collectorIDToSend。
有没有办法解决这个问题?
谢谢!
英文:
I need map the struct to create a JSON structure. The collector_id attribute in JSON should be able to take null value or int value.
I hace the follow code:
type purchaseInfo struct {
CollectorID *int64 `json:"collector_id"`
}
func mapPurchaseInfo(collectorID int64) purchaseInfo {
var collectorIDToSend *int64
if collectorID < 0 {
collectorIDToSend = nil
} else {
collectorIDToSend = collectorID
}
return purchaseInfo{
CollectorID: collectorIDToSend,
}
}
This code doesn't compile, the collectorID can't be assigned to collectorIDToSend.
Is there a way to be able to do this?
Thanks!
答案1
得分: 2
在mapPurchaseInfo函数的声明中,为了将传入参数的值正确赋给collectorIDToSend,必须使用&运算符来获取collectorID的内存地址。
在构建purchaseInfo返回变量时,可以直接将结构的字段放在示例中。
type purchaseInfo struct {
CollectorID *int64 `json:"collector_id"`
}
func mapPurchaseInfo(collectorID int64) purchaseInfo {
var collectorIDToSend *int64
if collectorID < 0 {
collectorIDToSend = nil
} else {
collectorIDToSend = &collectorID
}
return purchaseInfo{
CollectorID: collectorIDToSend,
}
}
英文:
- In the declaration of the mapPurchaseInfo function, to correctly assign the value passed in parameter to collectorIDToSend, the & operator must be used to retrieve the memory address of collectorID.
- When constructing the purchaseInfo return variable, it is possible to put the fields of the structure directly as in the example.
type purchaseInfo struct {
CollectorID *int64 `json:"collector_id"`
}
func mapPurchaseInfo(collectorID int64) purchaseInfo {
var collectorIDToSend *int64
if collectorID < 0 {
collectorIDToSend = nil
} else {
collectorIDToSend = &collectorID
}
return purchaseInfo{
CollectorID: collectorIDToSend,
}
}
答案2
得分: -2
以下是翻译好的内容:
type purchaseInfo struct {
CollectorID *int64 `json:"collector_id"`
}
func mapPurchaseInfo(collectorID *int64) purchaseInfo {
if *collectorID < 0 {
return purchaseInfo{CollectorID: nil}
}
return purchaseInfo{
CollectorID: collectorID,
}
}
请注意,这是一段Go语言的代码,用于定义一个名为purchaseInfo
的结构体和一个名为mapPurchaseInfo
的函数。purchaseInfo
结构体包含一个名为CollectorID
的指向int64
类型的指针。mapPurchaseInfo
函数接受一个int64
类型的指针作为参数,并根据参数的值创建一个purchaseInfo
结构体的实例。如果参数的值小于0,则CollectorID
字段被设置为nil
,否则将使用参数的值初始化CollectorID
字段。
英文:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
type purchaseInfo struct {
CollectorID *int64 `json:"collector_id"`
}
func mapPurchaseInfo(collectorID *int64) purchaseInfo {
if *collectorID < 0 {
return purchaseInfo{CollectorID: nil}
}
return purchaseInfo{
CollectorID: collectorID,
}
}
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论