This works:
pressure := &dataDump[845]
CurrentPressure := *pressure
But is there a way to change the first line so that pressure
is an alias for dataDump[845]
and so no asterisk is needed:
CurrentPressure := pressure
This works:
pressure := &dataDump[845]
CurrentPressure := *pressure
But is there a way to change the first line so that pressure
is an alias for dataDump[845]
and so no asterisk is needed:
CurrentPressure := pressure
By "changing" data I mean if the dataDump
array / slice changes, you want your pressure
to reflect the changes.
This is not possible in Go. What you want would require to explicitly specify the memory address where a variable is to be created / placed.
Your best option is to use a pointer which you included in your question.
Another alternative would be to create a function, e.g.:
function pressure() int {
return dataDump[845]
}
And using it:
currentPressure := pressure()
If dataDump
doesn't change once it's acquired, then this is not a problem. You can use a simple non-pointer variable like this:
pressure := dataDump[845] // Not a pointer to the element but a copy of it
And then:
currentPressure := pressure
But then in this case currentPressure
isn't even needed, you can just use pressure
(or maybe name it currentPressure
in the first place).
I don't know how you acquire your data, but in some cases it is possible to provide the Go value where you want the data to be placed / unmarshaled. Such cases may be reading the data from a file, or from a TCP connection.
If this is you case, you may use a struct
carefully planning the memory layout of the data you get, and then you may use struct fields which you can declare to be non-pointers.
Example:
type dataDump struct {
_ [845]int32 // Some unused data
pressure int32
}
If you can "unmarshal" your data into a value of this struct, then you can obtain the current pressure like this:
dump := dataDump{}
// Unmarshal into dump
currentPressure := dump.pressure
If you go down this way, be aware of the Spec: Size and alignment guarantees. Care must be taken due to implicit alignments!
See related questions for more details about laying out memory: