需要帮助理解 Go 中的 `map[String]type` 行为 [英] Need help understanding `map[String]type` behaviour in Go
问题描述
请看一下这段代码:
package activity
import (
"fmt"
"strconv"
"time"
)
type Activity struct {
yearContributions map[string]weekContributions
}
type dayContributions struct {
Date time.Time
Contribution int
}
type weekContributions struct {
Notation string
AllDays []dayContributions
}
func (currentWeekContribution *weekContributions) addContribution(additionalDayContribution dayContributions) {
currentWeekContribution.AllDays = append(currentWeekContribution.AllDays, additionalDayContribution)
}
func (currentActivity *Activity) restore() {
currentActivity.yearContributions = map[string]weekContributions{
"Sunday": weekContributions{Notation: "S", AllDays: make([]dayContributions, 0)},
"Monday": weekContributions{Notation: "M", AllDays: make([]dayContributions, 0)},
"Tuesday": weekContributions{Notation: "T", AllDays: make([]dayContributions, 0)},
"Wednesday": weekContributions{Notation: "W", AllDays: make([]dayContributions, 0)},
"Thursday": weekContributions{Notation: "T", AllDays: make([]dayContributions, 0)},
"Friday": weekContributions{Notation: "F", AllDays: make([]dayContributions, 0)},
"Saturday": weekContributions{Notation: "S", AllDays: make([]dayContributions, 0)},
}
}
func (currentActivity *Activity) Recalibrate() {
currentActivity.restore()
endDate := time.Now().UTC()
startDate := endDate.AddDate(-1, 0, 0)
for d := startDate; endDate.After(d); d = d.AddDate(0, 0, 1) {
stringWeekday := d.Weekday().String()
currentWeekContribution := currentActivity.yearContributions[stringWeekday]
currentWeekContribution.addContribution(dayContributions{Date: d, Contribution: 1})
fmt.Println(currentWeekContribution) // prints : {T [{2017-01-26 09:39:45.711238257 +0000 UTC 1}]}
}
fmt.Println(currentActivity.yearContributions) // prints : map[Tuesday:{T []} Wednesday:{W []} Thursday:{T []} Friday:{F []} Saturday:{S []} Sunday:{S []} Monday:{M []}]
}
func (singleDayContribution dayContributions) ReadableDate() string {
singleDate := singleDayContribution.Date
return singleDate.Month().String() + " " + strconv.Itoa(singleDate.Day()) + ", " + strconv.Itoa(singleDate.Year())
}
func (currentActivity Activity) GetContributionsForYear() map[string]weekContributions {
return currentActivity.yearContributions
}
我正在尝试将新的 dayContributions
附加到数组 weekContributions.AllDays
.currentWeekContribution.AllDays = append(currentWeekContribution.AllDays, additionalDayContribution)
是 appends
到当前类型 weekContributions
AllDays
属性的一部分/代码>.
I am trying to append new dayContributions
to the array weekContributions.AllDays
.
currentWeekContribution.AllDays = append(currentWeekContribution.AllDays, additionalDayContribution)
is the part appends
to the AllDays
property of the current type weekContributions
.
print 语句打印 weekContributions
数组和数组中的一个成员.但是下面的打印函数表示所有地图值,即 weekContributions
对象都有空数组作为 AllDays
属性.
The print statement prints the weekContributions
array with a single member in the array. But the following print function says that all map values i.e. weekContributions
objects have empty arrays as AllDays
property.
我的目标 - 将 dayContribution
对象附加到地图 activity.yearContributions
My aim - To append dayContribution
objects to values in map activity.yearContributions
我认为 -
- append 函数按预期工作,因为它在每次迭代中附加 1 个成员
- 不知何故,
currentWeekContribution.addContribution
行 [第 48 行] 仅附加到局部变量currentWeekContribution
并且不会更新currentActivity 中的相应值.年贡献
地图
- The append function is working as intended as it appends 1 member on each iteration
- Somehow, the
currentWeekContribution.addContribution
line [Line 48] is only appending to the local variablecurrentWeekContribution
and doesn't update the corresponding value incurrentActivity.yearContributions
map
我对 golang
很陌生,我认为我没有遵循 go 方式
/这在 java
或 中有效>红宝石
我不知道这种行为叫什么,所以不能用谷歌搜索任何相关的东西.
I am very new to golang
and I think I am not following the go way
/ this would have worked in java
or ruby
I don't know what this behaviour is called, so can't google search anything relevant.
推荐答案
有两种方法可以做到这一点.
Two way you can do this.
一,您需要使用指针 in 作为 Map 值.因此,当您附加到局部变量时,您正在通过引用进行更改.
One, You need to use pointer in as Map value. So that when you are appending in your local variable, you are changing through reference.
type Activity struct {
yearContributions map[string]*weekContributions
}
type dayContributions struct {
Date time.Time
Contribution int
}
type weekContributions struct {
Notation string
AllDays []*dayContributions
}
二、当你在做以下事情时
Two, When you are doing following
currentWeekContribution := currentActivity.yearContributions[stringWeekday]
您正在分配一个局部变量.你正在附加它.现在,您需要重新分配该局部变量.喜欢
You are assigning a local variable. And you are appending to it. Now, you need to reassign that local variable. Like
currentWeekContribution := currentActivity.yearContributions[stringWeekday]
currentWeekContribution.addContribution(dayContributions{Date: d, Contribution: 1})
currentActivity.yearContributions[stringWeekday]= currentWeekContribution
这篇关于需要帮助理解 Go 中的 `map[String]type` 行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!