将 int64 值设置给 *int64 指针。

huangapple go评论78阅读模式
英文:

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:&quot;collector_id&quot;`
}

func mapPurchaseInfo(collectorID int64) purchaseInfo {
    var collectorIDToSend *int64
    if collectorID &lt; 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:&quot;collector_id&quot;`
    }

    func mapPurchaseInfo(collectorID int64) purchaseInfo {
        var collectorIDToSend *int64
        if collectorID &lt; 0 {
            collectorIDToSend = nil
        } else {
            collectorIDToSend = &amp;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:&quot;collector_id&quot;`
}

func mapPurchaseInfo(collectorID *int64) purchaseInfo {
	if *collectorID &lt; 0 {
		return purchaseInfo{CollectorID: nil}
	}
	return purchaseInfo{
		CollectorID: collectorID,
	}
}

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年1月12日 05:05:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75088993.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定