亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

迭代結構列表并更改成員變量

迭代結構列表并更改成員變量

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 關注
  • 196 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號