Home >Backend Development >Golang >Set an int64 value to an *int64 pointer
php editor Xigua is here to introduce to you a problem about setting an int64 value to an int64 pointer. In programming, we often need to manipulate pointers to modify the value of variables. For variables of type int64, we can modify their values by setting them to int64 pointers. This operation is very useful in certain situations and can help us process data more flexibly. Next, let us analyze this problem in detail and give corresponding sample code.
I need to map the structure to create a json structure. The collector_id attribute in json should be able to take null value or int value. I have the following 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 does not compile and collectorid cannot be assigned to collectoridtosend. Is there a way to do this?
Thanks!
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, } }
The above is the detailed content of Set an int64 value to an *int64 pointer. For more information, please follow other related articles on the PHP Chinese website!