DEV Community

samrajput1143
samrajput1143

Posted on

Iterate over the field of structs and modify the value (Golang)

type CellMetricsEntry struct {
DRBUEThpDl interface{}
DRBUEThpUl interface{}
QosFlowPdcpPduVolumeUl interface{}
QosFlowPdcpPduVolumeDl interface{}
}

In the above structure there are more than 100 variables, but here I have taken only 4.

in main function I somehow want to iterate over the field and want to modify it, as per my need.

The field values can either be an int32 or float64

How can i iterate over this structure field and modify the value.

I want to achieve something like

`var metric CellMetricsEntry

for (int i=0;i< no_of_elements;i++)//this is pseudo code
{
metric[i]=value//setting each field of struct in for loop
//the above value I will get from some other application and this may be //either int32 or float64

}`

After setting all the field , I will push it to Time series database (influxdb)
writecellMetrics_db(metric)

Top comments (0)