0

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
icza
  • 389,944
  • 63
  • 907
  • 827
David Smith
  • 892
  • 1
  • 6
  • 16
  • Remember that Go tries to be obvious and avoid surprising the programmer. Secret pointers are arguably rather surprising. – Zan Lynx Sep 30 '16 at 02:32
  • That maps and slices are already secret pointers is bad enough. – Zan Lynx Sep 30 '16 at 02:32
  • You can create a slice, but that's not any better: pressure := dataDump[845:846]; currentPressure = pressure[0] – Charlie Tumahai Sep 30 '16 at 03:14
  • Go's pointer stuff works pretty much like C's does modulo pointer arithmetic and the Tour of Go describes this pretty well. Remember that Go has strict rules for assignability of different types. – Volker Sep 30 '16 at 06:21
  • 1
    Why the downvote? How could I improve this question? – David Smith Sep 30 '16 at 06:59

1 Answers1

1

For "changing" data

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()

For "static" data

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).

Using memory layout

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:

Why use arrays instead of slices?

Why have arrays in Go?

Community
  • 1
  • 1
icza
  • 389,944
  • 63
  • 907
  • 827
  • Thanks, heaps for a great answer - looks like the "Using memory layout" method is the way to go. I'll do more reading. Just a question; I'm not familiar with the syntax of "_ [845]int32" which reserves the memory, do you have a link to the docs please? – David Smith Sep 30 '16 at 09:23
  • @DavidSmith `_` is the [blank identifier](https://golang.org/ref/spec#Blank_identifier). It is used if syntax requires an identifier, but you don't want to use the result. You could just use a real field name instead of that, e.g. `data [845]int32`. – icza Sep 30 '16 at 09:27