FAQ
I am trying to create a new Struct and store a pointer to its value in
memory inside a map. My code example:

type Device struct{

Data string
Servicable bool

}

func (d *Device) Update(data string, servicable bool){

d.Data = data
d.Servicable = servicable

}

Then sometime later I want to do the following:

var device_map = make(map[string]*devices.Device)

func update_device_map(device_name string, device_servicable bool){

if dev,mapped := device_map[device_name]; !mapped {

log.Printf("Creating new device: %v", device_name)

device_map[device_name] = &devices.Device { Data: device_name, Servicable:
device_servicable }

} else {

log.Printf("Updating device: %v", &dev)

dev.Update(device_name, device_servicable)

}

}

The log results:

2014/07/10 13:22:10 Creating new device: Room 1

2014/07/10 13:22:10 Creating new device: Room 2 (3)

2014/07/10 13:22:12 Updating device: 0xc208042068

2014/07/10 13:22:12 Updating device: 0xc208042070

2014/07/10 13:22:14 Updating device: 0xc208042098

2014/07/10 13:22:14 Updating device: 0xc2080420a0

2014/07/10 13:22:16 Updating device: 0xc2080420c8

2014/07/10 13:22:16 Updating device: 0xc2080420d0

2014/07/10 13:22:18 Updating device: 0xc2080420f8

2014/07/10 13:22:18 Updating device: 0xc208042100

Why are these locations all different every time I perform a look up on the
map?

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • David Symonds at Jul 11, 2014 at 3:17 am
    You are printing out the memory address of a local variable called "dev".

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Djadala at Jul 11, 2014 at 4:01 am
    log.Printf("Updating device: %v", unsafe.Pointer(dev))
    On Thursday, July 10, 2014 8:29:23 PM UTC+3, Mike K. wrote:

    I am trying to create a new Struct and store a pointer to its value in
    memory inside a map. My code example:

    type Device struct{

    Data string
    Servicable bool

    }

    func (d *Device) Update(data string, servicable bool){

    d.Data = data
    d.Servicable = servicable

    }

    Then sometime later I want to do the following:

    var device_map = make(map[string]*devices.Device)

    func update_device_map(device_name string, device_servicable bool){

    if dev,mapped := device_map[device_name]; !mapped {

    log.Printf("Creating new device: %v", device_name)

    device_map[device_name] = &devices.Device { Data: device_name, Servicable:
    device_servicable }

    } else {

    log.Printf("Updating device: %v", &dev)

    dev.Update(device_name, device_servicable)

    }

    }

    The log results:

    2014/07/10 13:22:10 Creating new device: Room 1

    2014/07/10 13:22:10 Creating new device: Room 2 (3)

    2014/07/10 13:22:12 Updating device: 0xc208042068

    2014/07/10 13:22:12 Updating device: 0xc208042070

    2014/07/10 13:22:14 Updating device: 0xc208042098

    2014/07/10 13:22:14 Updating device: 0xc2080420a0

    2014/07/10 13:22:16 Updating device: 0xc2080420c8

    2014/07/10 13:22:16 Updating device: 0xc2080420d0

    2014/07/10 13:22:18 Updating device: 0xc2080420f8

    2014/07/10 13:22:18 Updating device: 0xc208042100

    Why are these locations all different every time I perform a look up on
    the map?
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Andrewchamberss at Jul 11, 2014 at 4:02 am
    You are taking the address of the pointer. the thing you are printing is **devices.Device.
    You want this

    log.Printf("Updating device: %v", dev)
    On Friday, July 11, 2014 5:29:23 AM UTC+12, Mike K. wrote:

    I am trying to create a new Struct and store a pointer to its value in
    memory inside a map. My code example:

    type Device struct{

    Data string
    Servicable bool

    }

    func (d *Device) Update(data string, servicable bool){

    d.Data = data
    d.Servicable = servicable

    }

    Then sometime later I want to do the following:

    var device_map = make(map[string]*devices.Device)

    func update_device_map(device_name string, device_servicable bool){

    if dev,mapped := device_map[device_name]; !mapped {

    log.Printf("Creating new device: %v", device_name)

    device_map[device_name] = &devices.Device { Data: device_name, Servicable:
    device_servicable }

    } else {

    log.Printf("Updating device: %v", &dev)

    dev.Update(device_name, device_servicable)

    }

    }

    The log results:

    2014/07/10 13:22:10 Creating new device: Room 1

    2014/07/10 13:22:10 Creating new device: Room 2 (3)

    2014/07/10 13:22:12 Updating device: 0xc208042068

    2014/07/10 13:22:12 Updating device: 0xc208042070

    2014/07/10 13:22:14 Updating device: 0xc208042098

    2014/07/10 13:22:14 Updating device: 0xc2080420a0

    2014/07/10 13:22:16 Updating device: 0xc2080420c8

    2014/07/10 13:22:16 Updating device: 0xc2080420d0

    2014/07/10 13:22:18 Updating device: 0xc2080420f8

    2014/07/10 13:22:18 Updating device: 0xc208042100

    Why are these locations all different every time I perform a look up on
    the map?
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Carlos Castillo at Jul 11, 2014 at 5:41 am
    These examples should be in the
    playground: http://play.golang.org/p/2FBVSgtWkh

    Also, to print an address, such as the value of a *Device, use the "%p"
    printf verb (see example). See how there is now only two addresses for two
    rooms as I am printing the %p representation of the address taken from the
    map.
    On Thursday, July 10, 2014 9:02:37 PM UTC-7, andrewc...@gmail.com wrote:

    You are taking the address of the pointer. the thing you are printing is **devices.Device.
    You want this

    log.Printf("Updating device: %v", dev)
    On Friday, July 11, 2014 5:29:23 AM UTC+12, Mike K. wrote:

    I am trying to create a new Struct and store a pointer to its value in
    memory inside a map. My code example:

    type Device struct{

    Data string
    Servicable bool

    }

    func (d *Device) Update(data string, servicable bool){

    d.Data = data
    d.Servicable = servicable

    }

    Then sometime later I want to do the following:

    var device_map = make(map[string]*devices.Device)

    func update_device_map(device_name string, device_servicable bool){

    if dev,mapped := device_map[device_name]; !mapped {

    log.Printf("Creating new device: %v", device_name)

    device_map[device_name] = &devices.Device { Data: device_name,
    Servicable: device_servicable }

    } else {

    log.Printf("Updating device: %v", &dev)

    dev.Update(device_name, device_servicable)

    }

    }

    The log results:

    2014/07/10 13:22:10 Creating new device: Room 1

    2014/07/10 13:22:10 Creating new device: Room 2 (3)

    2014/07/10 13:22:12 Updating device: 0xc208042068

    2014/07/10 13:22:12 Updating device: 0xc208042070

    2014/07/10 13:22:14 Updating device: 0xc208042098

    2014/07/10 13:22:14 Updating device: 0xc2080420a0

    2014/07/10 13:22:16 Updating device: 0xc2080420c8

    2014/07/10 13:22:16 Updating device: 0xc2080420d0

    2014/07/10 13:22:18 Updating device: 0xc2080420f8

    2014/07/10 13:22:18 Updating device: 0xc208042100

    Why are these locations all different every time I perform a look up on
    the map?
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJul 11, '14 at 3:15a
activeJul 11, '14 at 5:41a
posts5
users5
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase