为了账号安全,请及时绑定邮箱和手机立即绑定

迭代结构列表并更改成员变量

迭代结构列表并更改成员变量

Go
蝴蝶刀刀 2023-08-14 14:29:58
请考虑代码https://play.golang.org/p/aO07_PoQLuh我有一个结构列表,我从中读取以了解我在途中生成的成员。对于每个结构,我都有一个提高计数器的方法,但是我缺少这里的酱汁。正如您从 o/p 中看到的那样,我已经增加了SBytesSent,但是当我读取结构列表并检查它时,它的值是 0。处理这个问题的最佳方法是什么?谢谢!package mainimport (    "fmt"    "sync")type destination struct {    Name          string    SBytesSent    int64    ABytesSent    int64    LastSeenAlive int64    Mutex         *sync.Mutex}type destinations []destinationvar (    destination_list destinations    myHosts          = []string{"host1", "host2", "host3"})func main() {    fmt.Println("Hello, playground")    for i, _ := range myHosts {        newDest := myHosts[i]        newd := destination{Name: newDest}        newd.Mutex = &sync.Mutex{}        destination_list = append(destination_list, newd)    }    i := 0    for {        increment()        status()        i += 1        if i == 3 {            break        }    }}func (self *destination) incrementSBytes(a int) {    self.Mutex.Lock()    defer self.Mutex.Unlock()    self.SBytesSent += int64(a)    fmt.Printf("new val %d\n", self.SBytesSent)}func (self *destination) Status() {    fmt.Printf("my val %d\n", self.SBytesSent)}func increment() {    for i, _ := range destination_list {        dest := destination_list[i]        dest.incrementSBytes(33)    }}func status() {    for i, _ := range destination_list {        dest := destination_list[i]        dest.Status()    }}编辑1请参阅https://play.golang.org/p/5uqqc3OKYDs - 我已经增加到host3-6但到最后他们都显示了99。如何让host3保留之前的增量并显示99 + 6 = 105?
查看完整描述

2 回答

?
江户川乱折腾

TA贡献1851条经验 获得超5个赞

它不起作用,因为您正在复制并修改副本:

dest := destination_list[i]
dest.incrementSBytes(33)

上面,您首先将数组元素复制到dest,然后修改dest。数组元素永远不会改变。而是尝试这个:

destination_list[i].incrementsSBytes(33)


查看完整回答
反对 回复 2023-08-14
?
慕标琳琳

TA贡献1830条经验 获得超9个赞

所有的魔力都在于range,它创建该元素的副本,因此修改是不可见的。


package main


import (

    "fmt"

    "sync"

)


type destination struct {

    Name          string

    SBytesSent    int64

    ABytesSent    int64

    LastSeenAlive int64

    Mutex         *sync.Mutex

}


type destinations []destination


var (

    destination_list destinations

    myHosts          = []string{"host1", "host2", "host3"}

)


func main() {

    fmt.Println("Hello, playground")

    for i := range myHosts {

        newDest := myHosts[i]

        newd := destination{Name: newDest}

        newd.Mutex = &sync.Mutex{}

        destination_list = append(destination_list, newd)

    }

    i := 0

    for {

        increment()

        status()

        i++

        if i == 3 {

            break

        }

    }


}


func (self *destination) incrementSBytes(a int) {

    self.Mutex.Lock()

    defer self.Mutex.Unlock()

    self.SBytesSent += int64(a)

    fmt.Printf("new val %d\n", self.SBytesSent)

}


func (self *destination) Status() {

    fmt.Printf("my val %d\n", self.SBytesSent)

}


func increment() {

    for i:=0; i<len(destination_list); i++ {

        destination_list[i].incrementSBytes(33)

    }

}


func status() {

    for i:=0; i<len(destination_list); i++ {

        destination_list[i].Status()

    }

}

输出:


Hello, playground

new val 33

new val 33

new val 33

my val 33

my val 33

my val 33

new val 66

new val 66

new val 66

my val 66

my val 66

my val 66

new val 99

new val 99

new val 99

my val 99

my val 99

my val 99


查看完整回答
反对 回复 2023-08-14
  • 2 回答
  • 0 关注
  • 90 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信